什么是SCP的Python中的文件中的最Python的方式? 我所知道的是必经之路
os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile) )
这是一个黑客,以及类似Linux系统外不工作,并且需要帮助从Pexpect的模块,以避免密码提示,除非你已经有密码的SSH建立到远程主机。
我知道Twisted的的conch
,但我宁愿避免通过低级别的ssh模块实现SCP自己。
我知道paramiko
,支持SSH和SFTP一个Python模块; 但它不支持SCP。
背景:我连接到不支持SFTP,但不支持SSH / SCP路由器,所以SFTP是不是一种选择。
编辑 :这是一个重复如何将文件复制到使用SCP或SSH Python中的远程服务器? 。 但是 ,这个问题并没有给出一个具体的SCP-答案,从蟒蛇内的钥匙交易。 我希望的方式来运行代码有点像
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')
Answer 1:
尝试将模块paramiko_scp 。 这是非常容易使用。 请看下面的例子:
import paramiko
from scp import SCPClient
def 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 client
ssh = createSSHClient(server, port, user, password)
scp = SCPClient(ssh.get_transport())
然后调用scp.get()或scp.put()做SCP业务。
( SCPClient代码 )
Answer 2:
你可能有兴趣尝试Pexpect的 ( 源代码 )。 这将允许你处理你的密码交互式提示。
下面是例如,从主要网站使用情况(FTP)的SNIP:
# This connects to the openbsd ftp site and
# downloads the recursive directory listing.
import pexpect
child = 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')
Answer 3:
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
Answer 4:
如果你在Win32安装腻子你会得到一个PSCP(SCP腻子)。
所以你可以使用Win32上使用os.system黑客了。
(你可以使用腻子剂的关键同治)
对不起,这只是一个黑客(但你可以在Python类包装它)
Answer 5:
找不到一个直接的答案,而这个“scp.Client”模块不存在。 相反, 这很适合我:
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')
Answer 6:
您可以使用包子和命令调用从外壳使用scp命令。
from subprocess import call
cmd = "scp user1@host1:files user2@host2:files"
call(cmd.split(" "))
Answer 7:
它已经有相当一段时间,因为这个问题是问,在此期间,可以处理这个其他图书馆也随之而来:您可以使用拷贝包含在功能铅库:
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)
Answer 8:
看一看fabric.transfer 。
from fabric import Connection
with Connection(host="hostname",
user="admin",
connect_kwargs={"key_filename": "/home/myuser/.ssh/private.key"}
) as c:
c.get('/foo/bar/file.txt', '/tmp/')
Answer 9:
截至今天,最好的解决办法可能是AsyncSSH
https://asyncssh.readthedocs.io/en/latest/#scp-client
async with asyncssh.connect('host.tld') as conn:
await asyncssh.scp((conn, 'example.txt'), '.', recurse=True)
Answer 10:
嗯,也许是另一种选择是使用像sshfs的 (有一个sshfs的为Mac太)。 一旦你的路由器安装你只可以直接复制文件。 我不知道是否适合你的特殊应用,但它是一个很好的解决方案,以保持得心应手。
Answer 11:
我前一段时间我放在一起依赖的paramiko一个Python SCP复制脚本。 它包含的代码来处理与私用密钥或SSH密钥剂后备密码验证的连接。
http://code.activestate.com/recipes/576810-copy-files-over-ssh-using-paramiko/
Answer 12:
如果你是在* nix可以使用sshpass
sshpass -p password scp -o User=username -o StrictHostKeyChecking=no src dst:/path
Answer 13:
我不认为有任何一个模块,您可以轻松地下载到实施SCP,但你可能会有所帮助: http://www.ibm.com/developerworks/linux/library/l-twist4.html
文章来源: How to scp in python?