Secure Copy File from remote server via scp and os module in Python
I think the easiest (to avoid having to enter a password) and most secure way to go about this is to first set public/private key authentication. Once that is done, and you can log in to the remote system by doing ssh user@hostname
, the following bash command would do the trick:
scp some/complete/path/to/file user@remote_system:some/remote/path
The corresponding Python code would be:
import subprocess
filepath = "some/complete/path/to/file"
hostname = "user@remote_system"
remote_path = "some/remote/path"
subprocess.call(['scp', filepath, ':'.join([hostname,remote_path])])
Admin
Updated on June 14, 2022Comments
-
Admin almost 2 years
I'm pretty new to Python and programming. I'm trying to copy a file between two computers via a python script. However the code
os.system("ssh " + hostname + " scp " + filepath + " " + user + "@" + localhost + ":" cwd)
won't work. I think it needs a password, as descriped in How to copy a file to a remote server in Python using SCP or SSH?. I didn't get any error logs, the file just won't show in my current working directory.
However every other command with
os.system("ssh " + hostname + "command")
oros.popen("ssh " + hostname + "command")
does work. ->command = e.g. ls
When I try
ssh hostname scp file user@local:directory
in the commandline it works without entering a password.I tried to combine
os.popen
commands with getpass and pxssh module to establish a ssh connection to the remote server and use it to send commands directly (I only tested it for an easy command):import pxssh import getpass ssh = pxssh.pxssh() ssh.force_password = True hostname = raw_input("Hostname: ") user = raw_input("Username: ") password = getpass.getpass("Password: ") ssh.login(hostname, user, password) test = os.popen("hostname") print test
But I'm not able to put commands through to the remote server (
print test
shows, that hostname = local and not the remote server), however I'm sure, the conection is established. I thought it would be easier to establish a connection than always use"ssh " + hostname
in the bash commands. I also tried some of the workarounds in How to copy a file to a remote server in Python using SCP or SSH?, but I must admit due to lack of expirience I didn't get them to work.Thanks a lot for helping me.