Nested SSH using Python Paramiko

I have this scenario:

Local host --------- jump-host ------- target computer

I am trying to write Python code using Paramiko for the first SSH from the local host to jump-host, and then SSH from jump-host to the target computer. From the target machine, I want to capture some output and save it locally, either as a variable or as a file (I have not reached this point yet). I found an example from where they talk about using embedded SSH with Paramiko, and I follow it, but I'm stuck here:

My code is:

enter code here #!/usr/bin/python # # Paramiko # import paramiko import sys import subprocess # # we instantiate a new object referencing paramiko SSHClient class # vm=paramiko.SSHClient() vm.set_missing_host_key_policy(paramiko.AutoAddPolicy()) vm.connect('192.168.115.103',username='osmanl',password='xxxxxx') # vmtransport = vm.get_transport() dest_addr = ('192.168.115.103', 22) local_addr = ('127.0.0.1', 22) vmchannel = vmtransport.open_channel("direct-tcpip", dest_addr, local_addr) # jhost=paramiko.SSHClient() jhost.set_missing_host_key_policy(paramiko.AutoAddPolicy()) jhost.load_host_keys('/home/osmanl/.ssh/known_hosts') jhost.connect('10.103.53.26', username='latiu', password='xxxx', sock=vmchannel) # stdin, stdout, stderr = rtr.exec_command("show version | no-more") # print stdout.readline() # jhost.close() vm.close() # End 

When I run above, I get this error:

 $ python sshvm.py Traceback (most recent call last): File "sshvm.py", line 28, in <module> jhost.load_host_keys('/home/osmanl/.ssh/known_hosts') File "/usr/lib/python2.7/site-packages/paramiko-1.15.2-py2.7.egg/paramiko/client.py", line 121, in load_host_keys self._host_keys.load(filename) File "/usr/lib/python2.7/site-packages/paramiko-1.15.2-py2.7.egg/paramiko/hostkeys.py", line 94, in load with open(filename, 'r') as f: IOError: [Errno 2] No such file or directory: '/home/osmanl/.ssh/known_hosts' 
+7
source share
3 answers

Try the following edited code, it should work:

 #!/usr/bin/python # # Paramiko # import paramiko import sys import subprocess # # we instantiate a new object referencing paramiko SSHClient class # vm = paramiko.SSHClient() vm.set_missing_host_key_policy(paramiko.AutoAddPolicy()) vm.connect('192.168.115.103', username='osmanl', password='xxxxxx') # vmtransport = vm.get_transport() dest_addr = ('10.103.53.26', 22) #edited# local_addr = ('192.168.115.103', 22) #edited# vmchannel = vmtransport.open_channel("direct-tcpip", dest_addr, local_addr) # jhost = paramiko.SSHClient() jhost.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #jhost.load_host_keys('/home/osmanl/.ssh/known_hosts') #disabled# jhost.connect('10.103.53.26', username='latiu', password='xxxx', sock=vmchannel) # stdin, stdout, stderr = jhost.exec_command("show version | no-more") #edited# # print stdout.read() #edited# # jhost.close() vm.close() # End 
+8
source

I want to connect to a Cisco Router (192.168.92.162) using the Cisco Intermediate Switch (192.168.92.163). based on the example above i used the following script

 import paramiko vm=paramiko.SSHClient() vm.set_missing_host_key_policy(paramiko.AutoAddPolicy()) vm.connect('192.168.92.163',username='user',password='cisco') vmtransport = vm.get_transport() dest_addr = ('192.168.92.162', 22) #edited# local_addr = ('192.168.92.163', 1234) #edited# vmchannel = vmtransport.open_channel("direct-tcpip", dest_addr, local_addr) jhost=paramiko.SSHClient() jhost.set_missing_host_key_policy(paramiko.AutoAddPolicy()) jhost.connect('192.168.92.162', username='cisco', password='cisco', sock=vmchannel) stdin, stdout, stderr = jhost.exec_command("show version") print (stdout.read()) #edited# jhost.close() vm.close() 

I get an error for the line (vmchannel = vmtransport.open_channel ("direct-tcpip", dest_addr, local_addr))

Secsh channel 0 open FAILED :: Unknown channel type File "C: \ Users \ user \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ paramiko \ transport.py", line 902, in open_channel raise e paramiko.ssh_exception.ChannelException: (3, "Unknown channel type")

0
source

I know that OP specifically asked Paramiko but I can do this very easily with fabric . Here is my solution

 from fabric import Connection out = Connection('host1').run('host2 uptime') print(out.stdout.strip()) 

This works great for me, and I have the output stored in a variable.

0
source

Source: https://habr.com/ru/post/1242667/


All Articles