我设法找到一个解决方案,但是需要一些手工工作。如果有人有更好的解决方案,请告诉我。
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.comn')buff = ''while not buff.endswith(''s password: '): resp = chan.recv(9999) buff += resp# Send the password and wait for a prompt.chan.send('secretn')buff = ''while not buff.endswith('some-prompt$ '): resp = chan.recv(9999) buff += resp# Execute whatever command and wait for a prompt again.chan.send('lsn')buff = ''while not buff.endswith('some-prompt$ '): resp = chan.recv(9999) buff += resp# Now buff has the data I need.print 'buff', buffssh.close()需要注意的是
t = ssh.get_transport()chan = t.open_session()chan.get_pty()
…你要这个
chan = ssh.invoke_shell()
这让我想起了我小时候放弃编写代码长达十年的尝试编写TradeWars脚本的过程。:)



