How to scp in Python? How to scp in Python? python python

How to scp in Python?


Try the Python scp module for Paramiko. It's very easy to use. See the following example:

import paramikofrom scp import SCPClientdef createSSHClient(server, port, user, password):    client = paramiko.SSHClient()    client.load_system_host_keys()    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())    client.connect(server, port, user, password)    return clientssh = createSSHClient(server, port, user, password)scp = SCPClient(ssh.get_transport())

Then call scp.get() or scp.put() to do SCP operations.

(SCPClient code)


You might be interested in trying Pexpect (source code). This would allow you to deal with interactive prompts for your password.

Here's a snip of example usage (for ftp) from the main website:

# This connects to the openbsd ftp site and# downloads the recursive directory listing.import pexpectchild = pexpect.spawn ('ftp ftp.openbsd.org')child.expect ('Name .*: ')child.sendline ('anonymous')child.expect ('Password:')child.sendline ('noah@example.com')child.expect ('ftp> ')child.sendline ('cd pub')child.expect('ftp> ')child.sendline ('get ls-lR.gz')child.expect('ftp> ')child.sendline ('bye')


You could also check out paramiko. There's no scp module (yet), but it fully supports sftp.

[EDIT]Sorry, missed the line where you mentioned paramiko.The following module is simply an implementation of the scp protocol for paramiko.If you don't want to use paramiko or conch (the only ssh implementations I know of for python), you could rework this to run over a regular ssh session using pipes.

scp.py for paramiko