如何在Python中进行scp?
在 Python 中 scp 文件的最 Pythonic 方法是什么? 我知道的唯一途径是,
os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile) )
这是一个 hack,并且在类似 Linux 的系统之外不起作用,并且需要 Pexpect 模块的帮助来避免密码提示,除非您已经为远程主机设置了无密码 SSH 。
我知道 Twisted 的 conch
,但我宁愿避免自己通过低级 ssh 模块实现 scp。
我知道 paramiko,这是一个支持 SSH 和 SFTP 的 Python 模块; 但它不支持SCP。
背景:我连接到的路由器不支持 SFTP,但支持 SSH/SCP,因此不能选择 SFTP。
编辑: 这是 如何使用 SCP 或 SSH 将文件复制到 Python 中的远程服务器?。 但是,这个问题并没有给出处理 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')
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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
尝试 Paramiko 的 Python scp 模块。 它非常容易使用。 请参见以下示例:
然后调用
scp.get()
或scp.put()
进行 SCP 操作。(SCPClient 代码)
Try the Python scp module for Paramiko. It's very easy to use. See the following example:
Then call
scp.get()
orscp.put()
to do SCP operations.(SCPClient code)
您可能有兴趣尝试 Pexpect (Pexpect) com/pexpect/pexpect" rel="nofollow noreferrer">源代码)。 这将允许您处理密码的交互式提示。
以下是来自主网站的示例用法(用于 ftp)的片段:
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:
找不到直接的答案,并且这个“scp.Client”模块不存在。
相反,这个适合我:
Couldn't find a straight answer, and this "scp.Client" module doesn't exist.
Instead, this suits me:
您还可以查看 paramiko。 目前还没有 scp 模块,但它完全支持 sftp。
[编辑]
抱歉,错过了您提到帕里科的那一行。
以下模块只是 paramiko 的 scp 协议的实现。
如果你不想使用 paramiko 或 conch (我所知道的 python 的唯一 ssh 实现),你可以修改它以使用管道在常规 ssh 会话上运行。
paramiko 的 scp.py
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
使用
paramiko
内置 sftp 客户端的简单示例。 这不仅仅是纯粹的 SCP,而且还在表面下使用 SSH,并且只要 SCP 工作就应该工作。 正如 OP 所说,他的路由器阻止了 SFTP。 因此,如果您绝对需要使用 SCP 协议,那么此解决方案不适合您。使用 SFTP 的好处:例如,您将能够列出目录,而不是仅限于放置/获取。 好处 2,仅使用 paramiko 可以减少依赖性。
Simple example using
paramiko
's built in sftp client. That is not pure SCP only, but uses SSH below the surface as well, and should work whenever SCP works. As the OP states, his router prevents SFTP. So, if you absolutly need to use the SCP protocol, this solution is not for you.Bonus using SFTP: you'll be able to list dirs for example, instead of being limited to put/get. Bonus 2, using only
paramiko
is reducing dependencies.如果你在 win32 上安装 putty,你会得到一个 pscp (putty scp)。
所以你也可以在 win32 上使用 os.system hack。
(你可以使用 putty-agent 进行密钥管理)
抱歉,这只是一个 hack
(但你可以将它包装在 python 类中)
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)
截至今天,最好的解决方案可能是
AsyncSSH
https:// /asyncssh.readthedocs.io/en/latest/#scp-client
As of today, the best solution is probably
AsyncSSH
https://asyncssh.readthedocs.io/en/latest/#scp-client
查看 fabric.transfer。
Have a look at fabric.transfer.
您可以使用包子进程和命令调用来从 shell 中使用 scp 命令。
You can use the package subprocess and the command call to use the scp command from the shell.
自从提出这个问题以来已经有一段时间了,与此同时,另一个可以处理这个问题的库已经出现:
您可以使用复制功能包含在 Plumbum 库中:
It has been quite a while since this question was asked, and in the meantime, another library that can handle this has cropped up:
You can use the copy function included in the Plumbum library:
如果您使用 *nix,则可以使用 sshpass
If you are on *nix you can use sshpass
嗯,也许另一种选择是使用类似 sshfs (有一个 sshfs 对于 Mac 也是如此)。 安装路由器后,您只需直接复制文件即可。 我不确定这是否适用于您的特定应用程序,但这是一个方便携带的好解决方案。
Hmmm, perhaps another option would be to use something like sshfs (there an sshfs for Mac too). Once your router is mounted you can just copy the files outright. I'm not sure if that works for your particular application but it's a nice solution to keep handy.
我不久前编写了一个依赖于 paramiko 的 python SCP 复制脚本。 它包含用于处理与私钥或 SSH 密钥代理的连接的代码,并回退到密码身份验证。
http://code.activestate.com/recipes/ 576810-通过-ssh-使用-paramiko复制文件/
I while ago I put together a python SCP copy script that depends on paramiko. It includes code to handle connections with a private key or SSH key agent with a fallback to password authentication.
http://code.activestate.com/recipes/576810-copy-files-over-ssh-using-paramiko/