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 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 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")
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
Loading key from an SSH jumphost using Paramiko
Yes. You have to have all credentials locally. The port forwarding alone does not make credentials stored on the intermediate hosts available for authentication.
Of course, you can use SFTP to access/download the files/keys, like:
sftp3 = ssh3.open_sftp()
with sftp3.open(".ssh/id_rsa") as key_file:
pkey = RSAKey.from_private_key(key_file)
ssh4.connect(host4, username=host4_username, sock=vmchannel, pkey=pkey)
Related Topics
Database Does Not Update Automatically with MySQL and Python
How to Serve Multiple Clients Using Just Flask App.Run() as Standalone
How to Send Non-English Unicode String Using Http Header
Is There a Python Equivalent for Rspec to Do Tdd
How Does Python Manage Int and Long
Python 32-Bit Memory Limits on 64Bit Windows
Using a Pre-Trained Word Embedding (Word2Vec or Glove) in Tensorflow
Can Modules Have Properties the Same Way That Objects Can
Pandas Extract Number from String
How to Create Nested Dict in Python
How to Compile Opencv for iOS7 (Arm64)
Learning Python from Ruby; Differences and Similarities
The Simplest Possible Reverse Proxy
Vscode -- How to Set Working Directory for Debugging a Python Program
Python Popen Command. Wait Until the Command Is Finished
Module' Object Has No Attribute 'Loads' While Parsing JSON Using Python