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)')
The
SSHClient.exec_commandmaps to "exec" channel of the SSH server. The "exec" channel typically runs shell commands. While you are trying to run Python code. Either use theexec_commandto runftp. Or runpythonwith your code (if the server haspython).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:
Nested SSH using Python Paramiko
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.