2017-04-02 147 views
1

基本上,我在Python中通过嵌套的SSH ssh会话执行scp时遇到了问题。我使用paramiko来建立从本地机器到另一个服务器的SSH,我们称之为AA。我想要scp文件,我们称之为f到服务器B。两台服务器都有相同的密码。这是代码:在python的paramiko中嵌套的ssh会话中的scp

ssh = paramiko.SSHClient() 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
ssh.connect(hostname=hostname, username=username, password=password) 

chan = ssh.invoke_shell()  
chan.send('scp f [email protected]:.') 
buff = '' 
while not buff.endswith('\'s password: '): 
    resp = chan.recv(9999) 
    buff += resp 

chan.send(password + '\n') 
buff = '' 
while not buff.endswith('$ '): 
    resp = chan.recv(9999) 
    buff += resp 

我真的不知道为什么这不起作用。任何帮助表示赞赏,非常感谢!

回答

0

你在PyCharm中运行它吗?它在什么时候失败,并且你有什么错误?这可能是

buff.endswith('\'s password: '): 

中的字符串与ssh会话中输出的内容不完全匹配。除此之外,它看起来是正确的。我会建议在buff.endswith中检测一下,并检查buff中的实际内容,并检查它是否与你的字符串匹配,以及如果你的密码对那里的换行符有效。