Skip to content Skip to sidebar Skip to footer

Secure Copy File From Remote Server Via Scp And Os Module In Python

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 + '

Solution 1:

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:

importsubprocessfilepath="some/complete/path/to/file"
hostname = "user@remote_system"
remote_path = "some/remote/path"

subprocess.call(['scp', filepath, ':'.join([hostname,remote_path])])

Post a Comment for "Secure Copy File From Remote Server Via Scp And Os Module In Python"