Nested SSH session with Paramiko
I managed to find a solution, but it requires a little manual work. If anyone have a better solution, please tell me.
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('first.com', username='luser', password='secret')
chan = ssh.invoke_shell()
# Ssh and wait for the password prompt.
chan.send('ssh second.com\n')
buff = ''
while not buff.endswith('\'s password: '):
resp = chan.recv(9999)
buff += resp
# Send the password and wait for a prompt.
chan.send('secret\n')
buff = ''
while not buff.endswith('some-prompt$ '):
resp = chan.recv(9999)
buff += resp
# Execute whatever command and wait for a prompt again.
chan.send('ls\n')
buff = ''
while not buff.endswith('some-prompt$ '):
resp = chan.recv(9999)
buff += resp
# Now buff has the data I need.
print 'buff', buff
ssh.close()
The thing to note is that instead of this
t = ssh.get_transport()
chan = t.open_session()
chan.get_pty()
...you want this
chan = ssh.invoke_shell()
It reminds me of when I tried to write a TradeWars script when I was a kid and gave up coding for ten years. :)
Nested SSH using Python Paramiko
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
Nested SSH with Paramiko and RSA key file
You cannot use port forwarding to implement the jump, if you need to use a private key stored on the jump server.
Either download the key to the local machine. If you do not want to physically store the key on the local machine, you can download it to memory in your Python code only. See Loading key from an SSH jumphost using Paramiko.
Otherwise you would have to implement the jump by running
ssh
client on the jump server, which will pick up the private key stored there (what is normally a lame solution):ssh.exec_command("ssh root@Y pwd")
Python SSH paramiko issue - ssh from inside of ssh session
can't you call the ssh
command from inside of your client.exec_command?
like:
client.exec_command('ssh user@host2 "apt-get install sl -y --force-yes"')
Related Topics
Error Installing Psycopg2 on MACos 10.9.5
How to Convert a Scikit-Learn Dataset to a Pandas Dataset
How to Make Sessions Timeout in Flask
Convert a Python Dict to a String and Back
Format Strings VS Concatenation
How to Set Layer-Wise Learning Rate in Tensorflow
Is There a Multi-Dimensional Version of Arange/Linspace in Numpy
Bin Size in Matplotlib (Histogram)
Putting Many Python Pandas Dataframes to One Excel Worksheet
How to Get Char from String by Index
How to Write Strategy Pattern in Python Differently Than Example in Wikipedia
Check If a Given Key Already Exists in a Dictionary and Increment It
How to Add a Custom Loglevel to Python's Logging Facility
Multiplying Across in a Numpy Array