How to copy a file to a remote server in Python using SCP or SSH? How to copy a file to a remote server in Python using SCP or SSH? python python

How to copy a file to a remote server in Python using SCP or SSH?


To do this in Python (i.e. not wrapping scp through subprocess.Popen or similar) with the Paramiko library, you would do something like this:

import osimport paramikossh = paramiko.SSHClient() ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))ssh.connect(server, username=username, password=password)sftp = ssh.open_sftp()sftp.put(localpath, remotepath)sftp.close()ssh.close()

(You would probably want to deal with unknown hosts, errors, creating any directories necessary, and so on).


You can call the scp bash command (it copies files over SSH) with subprocess.run:

import subprocesssubprocess.run(["scp", FILE, "USER@SERVER:PATH"])#e.g. subprocess.run(["scp", "foo.bar", "joe@srvr.net:/path/to/foo.bar"])

If you're creating the file that you want to send in the same Python program, you'll want to call subprocess.run command outside the with block you're using to open the file (or call .close() on the file first if you're not using a with block), so you know it's flushed to disk from Python.

You need to generate (on the source machine) and install (on the destination machine) an ssh key beforehand so that the scp automatically gets authenticated with your public ssh key (in other words, so your script doesn't ask for a password).


You'd probably use the subprocess module. Something like this:

import subprocessp = subprocess.Popen(["scp", myfile, destination])sts = os.waitpid(p.pid, 0)

Where destination is probably of the form user@remotehost:remotepath. Thanks to@Charles Duffy for pointing out the weakness in my original answer, which used a single string argument to specify the scp operation shell=True - that wouldn't handle whitespace in paths.

The module documentation has examples of error checking that you may want to perform in conjunction with this operation.

Ensure that you've set up proper credentials so that you can perform an unattended, passwordless scp between the machines. There is a stackoverflow question for this already.