without - python subprocess scp password
How to scp in python? (9)
As of today, the best solution is probably
async with asyncssh.connect('host.tld') as conn: await asyncssh.scp((conn, 'example.txt'), '.', recurse=True)
What's the most pythonic way to scp a file in Python? The only route I'm aware of is
os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile) )
which is a hack, and which doesn't work outside linux-like systems, and which needs help from the Pexpect module to avoid password prompts unless you already have passwordless SSH set up to the remote host.
I'm aware of Twisted's
conch, but I'd prefer to avoid implementing scp myself via low-level ssh modules.
I'm aware of
paramiko, a Python module that supports ssh and sftp; but it doesn't support scp.
Background: I'm connecting to a router which doesn't support sftp but does support ssh/scp, so sftp isn't an option.
EDIT: This is a duplicate of How to copy a file to a remote server in Python using SCP or SSH?. However, that question doesn't give an scp-specific answer that deals with keys from within python. I'm hoping for a way to run code kind of like
import scp client = scp.Client(host=host, user=user, keyfile=keyfile) # or client = scp.Client(host=host, user=user) client.use_system_keys() # or client = scp.Client(host=host, user=user, password=password) # and then client.transfer('/etc/local/filename', '/etc/remote/filename')
Couldn't find a straight answer, and this "scp.Client" module doesn't exist. Instead, this suits me:
from paramiko import SSHClient from scp import SCPClient ssh = SSHClient() ssh.load_system_host_keys() ssh.connect('example.com') with SCPClient(ssh.get_transport()) as scp: scp.put('test.txt', 'test2.txt') scp.get('test2.txt')
I don't think there's any one module that you can easily download to implement scp, however you might find this helpful: http://www.ibm.com/developerworks/linux/library/l-twist4.html
If you are on *nix you can use sshpass
sshpass -p password scp -o User=username -o StrictHostKeyChecking=no src dst:/path
import plumbum r = plumbum.machines.SshMachine("example.net") # this will use your ssh config as `ssh` from shell # depending on your config, you might also need additional # params, eg: `user="username", keyfile=".ssh/some_key"` fro = plumbum.local.path("some_file") to = r.path("/path/to/destination/") plumbum.path.utils.copy(fro, to)
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.
if you install putty on win32 you get an pscp (putty scp).
so you can use the os.system hack on win32 too.
(and you can use the putty-agent for key-managment)
sorry it is only a hack (but you can wrap it in a python class)