简体   繁体   English

scp在python的paramiko中的嵌套ssh会话中

[英]scp in a nested ssh session in python's paramiko

Basically, I have an issue with performing an scp over a nested SSH ssh session in Python. 基本上,我在Python中使用嵌套的SSH ssh会话执行scp时遇到问题。 I used paramiko to establish an SSH from my local machine to another server, let's call this A and from A . 我使用paramiko从我的本地机器到另一台服务器建立一个SSH,让我们称之为AA。 I want to scp a file, let's call it f to a server B . 我想SCP的文件,我们称之为F到服务器B中 Both servers have the same password. 两台服务器都有相同的密码。 This is the code: 这是代码:

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 username@B:.')
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

I'm really not sure why this doesn't work. 我真的不确定为什么这不起作用。 Any help is appreciated, thanks a lot! 感谢任何帮助,非常感谢!

Are you running this in PyCharm? 你在PyCharm中运行它吗? At what point does it fail, and are you getting any errors? 它什么时候失败,你有任何错误吗? It could be that the string in 它可能是字符串中的

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

doesn't exactly match what's being outputted in the ssh session. 与ssh会话中输出的内容不完全匹配。 Other than that it looks correct. 除此之外它看起来是正确的。 I would recommend breakpointing at buff.endswith and inspecting whats actually in the buff and checking to see if it matches your string, as well as if your password is valid with that newline character in there. 我建议在buff.endswith中进行breakpointing并检查buff实际上是什么,并检查它是否与你的字符串匹配,以及你的密码是否与那里的换行符有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM