I have this scenario:
Local-host --------- jump-host ------- target-machine
I am trying to write a code in Python using Paramiko to first SSH from local-host
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's 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
I know OP has specifically asked for 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 just fine for me, and i have the output stored in a variable as well.
I found this easiest route to login to remote server via jumphost.This works amazing!
link :https://pypi.org/project/jumpssh/
import jumpssh