Running ftplib code on remote server with Paramiko.

huangapple go评论59阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月17日 18:39:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定