英文:
Running ftplib code on remote server with Paramiko
问题
Step 1 (from server1): ssh server2
(从服务器1执行)
Step 2 (from server2): ftp server3
(从服务器2执行)
如何执行相同操作?
import paramiko
import ftplib
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(server2)
(stdin, stdout, stderr) = client.exec_command('ftp_server = ftplib.FTP(server3, user, pass) ; ftp_server.encoding = "utf-8" ; ftp_server.cwd("xx/yy") ; file=open("aa.png", "rb") ; ftp_server.storbinary(f"STOR {"aa.png"}", file)')
英文:
I need to execute the below commands in the order
Step 1 (from server1): ssh server2
Step 2 (from server2): ftp server3
How can I do the same?
import paramiko
import ftplib
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(server2)
(stdin, stdout, stderr) = client.exec_command('ftp_server = ftplib.FTP(server3, user, pass) ; ftp_server.encoding = "utf-8" ; ftp_server.cwd("xx/yy") ; file=open("aa.png", "rb") ; ftp_server.storbinary(f"STOR {"aa.png"}", file)')
答案1
得分: 1
The SSHClient.exec_command
maps to "exec" channel of the SSH server. The "exec" channel typically runs shell commands. While you are trying to run Python code. Either use the exec_command
to run ftp
. Or run python
with your code (if the server has python
).
Though the native Python solution would be to forward the local ports and run the Python FTP code locally to against the forwarded ports. Like here:
https://stackoverflow.com/q/35304525/850848
Except that you will use nesting FTP, not SSH. But port forwarding FTP is more difficult, as you need to forward even the data ports.
英文:
The SSHClient.exec_command
maps to "exec" channel of the SSH server. The "exec" channel typically runs shell commands. While you are trying to run Python code. Either use the exec_command
to run ftp
. Or run python
with your code (if the server has python
).
Though the native Python solution would be to forward the local ports and run the Python FTP code locally to against the forwarded ports. Like here:
https://stackoverflow.com/q/35304525/850848
Except that you will use nesting FTP, not SSH. But port forwarding FTP is more difficult, as you need to forward even the data ports.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论