簡體   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