使用Paramiko的嵌套SSH会话
我正在把我写的一个Bash脚本重写成Python。这个脚本的核心内容是
ssh -t first.com "ssh second.com very_remote_command"
我在使用paramiko进行嵌套认证时遇到了问题。我找不到任何与我具体情况相关的例子,但我找到了一些关于在远程主机上使用sudo的例子。
第一种方法是通过标准输入(stdin)写入
ssh.connect('127.0.0.1', username='jesse', password='lol')
stdin, stdout, stderr = ssh.exec_command("sudo dmesg")
stdin.write('lol\n')
stdin.flush()
第二种方法是创建一个通道,并使用类似于套接字的send和recv。
我能让stdin.write在sudo下工作,但在远程主机的ssh上却不行。
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('first.com', username='luser', password='secret')
stdin, stdout, stderr = ssh.exec_command('ssh luser@second.com')
stdin.write('secret')
stdin.flush()
print '---- out ----'
print stdout.readlines()
print '---- error ----'
print stderr.readlines()
ssh.close()
...打印...
---- out ----
[]
---- error ----
['Pseudo-terminal will not be allocated because stdin is not a terminal.\r\n', 'Permission denied, please try again.\r\n', 'Permission denied, please try again.\r\n', 'Permission denied (publickey,password,keyboard-interactive).\r\n']
伪终端错误让我想起了我原始命令中的-t标志,所以我切换到了第二种方法,使用通道。与ssh.exec_command不同,我现在有:
t = ssh.get_transport()
chan = t.open_session()
chan.get_pty()
print '---- send ssh cmd ----'
print chan.send('ssh luser@second.com')
print '---- recv ----'
print chan.recv(9999)
chan = t.open_session()
print '---- send password ----'
print chan.send('secret')
print '---- recv ----'
print chan.recv(9999)
...但它打印'---- send ssh cmd ----'后就挂住了,直到我杀掉这个进程。
我对Python还很陌生,对网络知识也不太了解。在第一种情况下,为什么发送密码在sudo下能工作,但在ssh下却不行?提示信息有什么不同吗?paramiko真的是适合这个的库吗?
5 个回答
7
你可以通过一个已经建立的ssh连接来创建另一个ssh连接。想了解更多细节,可以查看这里。
17
这里有一个简单的例子,只使用了paramiko(还有端口转发):
import paramiko as ssh
class SSHTool():
def __init__(self, host, user, auth,
via=None, via_user=None, via_auth=None):
if via:
t0 = ssh.Transport(via)
t0.start_client()
t0.auth_password(via_user, via_auth)
# setup forwarding from 127.0.0.1:<free_random_port> to |host|
channel = t0.open_channel('direct-tcpip', host, ('127.0.0.1', 0))
self.transport = ssh.Transport(channel)
else:
self.transport = ssh.Transport(host)
self.transport.start_client()
self.transport.auth_password(user, auth)
def run(self, cmd):
ch = self.transport.open_session()
ch.set_combine_stderr(True)
ch.exec_command(cmd)
retcode = ch.recv_exit_status()
buf = ''
while ch.recv_ready():
buf += ch.recv(1024)
return (buf, retcode)
# The example below is equivalent to
# $ ssh 10.10.10.10 ssh 192.168.1.1 uname -a
# The code above works as if these 2 commands were executed:
# $ ssh -L <free_random_port>:192.168.1.1:22 10.10.10.10
# $ ssh 127.0.0.1:<free_random_port> uname -a
host = ('192.168.1.1', 22)
via_host = ('10.10.10.10', 22)
ssht = SSHTool(host, 'user1', 'pass1',
via=via_host, via_user='user2', via_auth='pass2')
print ssht.run('uname -a')
33
我找到了解决办法,不过需要一点手动操作。如果有人有更好的办法,请告诉我。
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()
需要注意的是,不要用这个
t = ssh.get_transport()
chan = t.open_session()
chan.get_pty()
...你应该用这个
chan = ssh.invoke_shell()
这让我想起了我小时候尝试写一个TradeWars脚本时,结果放弃了编程整整十年。:)