3

I have a code where I have to login into a Unix server. After that I have to sftp into some server and have download few files to that Unix server. I am using Pythons' Paramiko command to login into Unix server. I know by using sftp.get(filepath, localpath), I can sftp files from SFTP server to local machine. However, my problem is I have to sftp those files into Unix server and not into local machine.

import paramiko

ip = ip
username = username
password = password
port = port


ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)
remotefilepath = remotefilepath
unixserverlocalpath = unixserverlocalpath

transport = paramiko.Transport(host)
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)

stdin,stdout,stderr=ssh.exec_command('some commands')
sftp.get(filepath, localpath)
stdin.write('Password')
stdin.flush()
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)
sftp.close()
transport.close()

This code is throwing error as it is trying to sftp files in my local machine rather than in unix server.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
vaishali bisht
  • 191
  • 2
  • 11

1 Answers1

1

SFTP does not allow transfer between two remote servers. So you have to initiate the transfer on one of the servers using any SFTP client available there.


If the servers are common *nix servers, they do have OpenSSH sftp client.

So use SSHClient.exec_command to execute sftp and then use stdin.write to feed sftp commands to it.

Like:

stdin,stdout,stderr = ssh.exec_command('sftp username@hostname')
stdin.write("get file\n")
stdin.write("quit\n")
stdin.flush()

Though the above will work only, if you have OpenSSH public key authentication setup (or any other input-less authentication) on the server, where you run sftp. With a password authentication, it's more complicated as OpenSSH does not allow you to provide a password automatically. You should be able to write the password to stdin, if you set get_pty=True:

stdin,stdout,stderr = ssh.exec_command('sftp username@hostname', get_pty=True)
stdin.write(password + "\n")
stdin.write("get file\n")
stdin.write("quit\n")
stdin.flush()

get_pty can bring some undesired side effects, but I believe that in this case it should work without problems.

For alternative approaches, see How to run the sftp command with a password from Bash script?


You can also use any other SFTP client, that you may have available on the servers, like curl or lftp.

Or you can execute python on one of the servers and feed Paramiko/SFTP code to it.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846