【问题标题】:Port forwarding and the open SFTP using Python Paramiko端口转发和使用 Python Paramiko 的开放式 SFTP
【发布时间】:2020-11-13 08:14:46
【问题描述】:

我已经使用 ssh 在服务器上执行了命令。现在我必须在保持旧 ssh 处于活动状态的同时对不同的 IP 执行另一个 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()

【问题讨论】:

    标签: python sftp paramiko portforwarding ssh-tunnel


    【解决方案1】:

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

    有关正确代码,请参阅Nested SSH using Python Paramiko

    如果您需要 SFTP,而不是 shell,只需:

    jhost.open_sftp()
    

    而不是

    jhost.exec_command(command)
    

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

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 2012-11-14
      • 2017-01-22
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      相关资源
      最近更新 更多