在Python中scp文件的最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不是一个选项。

编辑: 这是如何在Python中使用SCP或SSH将文件复制到远程服务器的副本。然而,这个问题并没有给出一个特定于scp的答案来处理Python内部的密钥。我希望有一种方法来运行代码

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

当前回答

这个问题被提出已经有一段时间了,与此同时,另一个可以处理这个问题的库突然出现了: 你可以使用Plumbum库中包含的复制函数:

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)

其他回答

嗯,也许另一个选择是使用类似sshfs的东西(Mac也有sshfs)。一旦你的路由器挂载,你可以直接复制文件。我不确定这是否适用于你的特定应用,但这是一个很好的解决方案。

您可能有兴趣尝试Pexpect(源代码)。这将允许您处理输入密码的交互式提示。

以下是来自主网站的ftp使用示例片段:

# 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')

您可以使用包子进程和命令调用从shell中使用scp命令。

from subprocess import call

cmd = "scp user1@host1:files user2@host2:files"
call(cmd.split(" "))

你也可以看看paramiko。目前还没有scp模块,但它完全支持sftp。

(编辑) 抱歉,我忘了你提到帕拉米科了。 下面的模块只是paramiko的scp协议的实现。 如果您不想使用paramiko或conch(我所知道的python的唯一ssh实现),您可以重新工作,使用管道在常规ssh会话上运行。

释义 scp.py

import paramiko

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect('<IP Address>', username='<User Name>',password='' ,key_filename='<.PEM File path')

#Setup sftp connection and transmit this script 
print ("copying")

sftp = client.open_sftp() 
sftp.put(<Source>, <Destination>)


sftp.close()