繁体   English   中英

端口转发和使用 Python Paramiko 的开放式 SFTP

[英]Port forwarding and the open SFTP using Python Paramiko

我已经使用 ssh 在服务器上执行了命令。 现在我必须对不同的 IP 再做一次 ssh,同时保持旧的 ssh 处于活动状态。

这个新的 IP 是端口转发,然后将用于执行 SFTP。

我面临的问题是 ssh 连接都在同一个端口上,因此无法进行第二个 ssh。 这是失败的SFTP。

对相同的任何支持都会有所帮助。

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=username, password=password, port=22)
time.sleep(3)
#Invoke shell to open multiple channel with in one SSH
chan = ssh.invoke_shell()
chan.send(ip1+'\n')
time.sleep(5)
chan.send(pass1+'\n')
time.sleep(10)

chan.send("ssh "+ip2+'\n')

time.sleep(10)
chan.send(pass2+'\n')
time.sleep(5)
#Execute command
chan.send(cmd)

#connect to another ip to do sftp
ssh1 = paramiko.SSHClient()
ssh1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("127.0.0.4", username=usr2, password=pass2, port=22)
sftp=ssh.open_sftp()

看来是误会了。 您的代码不进行任何端口转发。

有关正确的代码,请参阅嵌套 SSH 使用 Python Paramiko

如果您需要 SFTP,而不是 shell,只需执行以下操作:

jhost.open_sftp()

代替

jhost.exec_command(command)

强制性警告:不要使用AutoAddPolicy这样做会失去对MITM 攻击的保护。 有关正确的解决方案,请参阅Paramiko "Unknown Server"

暂无
暂无

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

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