Skip to content

Commit af7b7b5

Browse files
authored
Merge pull request #10 from easyawslearn/paramiko
nested paramiko example added
2 parents 92d6992 + 990a678 commit af7b7b5

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Diff for: Python-Paramiko/nested-paramiko.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import paramiko
2+
import sys
3+
import subprocess
4+
#
5+
# we instantiate a new object referencing paramiko's SSHClient class
6+
#
7+
jumphost = "192.168.1.10"
8+
server = "192.168.1.11"
9+
10+
vm = paramiko.SSHClient()
11+
vm.set_missing_host_key_policy(paramiko.AutoAddPolicy())
12+
vm.connect(jumphost, username='root', password='a')
13+
#
14+
vmtransport = vm.get_transport()
15+
server_addr = (server, 22) #edited#
16+
jump_host = (jumphost, 22) #edited#
17+
vmchannel = vmtransport.open_channel("direct-tcpip", server_addr, jump_host)
18+
#
19+
jhost = paramiko.SSHClient()
20+
jhost.set_missing_host_key_policy(paramiko.AutoAddPolicy())
21+
#jhost.load_host_keys('/home/osmanl/.ssh/known_hosts') #disabled#
22+
jhost.connect(server, username='root', password='a', sock=vmchannel)
23+
#
24+
stdin, stdout, stderr = jhost.exec_command("show version | no-more") #edited#
25+
stdin, stdout, stderr = jhost.exec_command("ifconfig") #edited#
26+
#
27+
print stdout.read() #edited#
28+
#
29+
jhost.close()
30+
vm.close()

Diff for: Python-Paramiko/subprocess-example.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import subprocess
2+
3+
logile="subprocess.log"
4+
def subprocess_cmd(command):
5+
print (command)
6+
process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
7+
proc_stdout = process.communicate()[0].strip()
8+
with open(logile,'w') as f:
9+
f.writelines(proc_stdout.split(' '))
10+
return logile
11+
12+
subprocess_cmd("uname")

0 commit comments

Comments
 (0)