Mic*_*ach 147 python ssh scp paramiko
在Python中scp文件的最pythonic方法是什么?我所知道的唯一途径是
os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile) )
Run Code Online (Sandbox Code Playgroud)
这是一个hack,并且在类似Linux的系统之外不起作用,并且需要Pexpect模块的帮助以避免密码提示,除非您已经将无密码SSH设置到远程主机.
我知道Twisted的conch
,但我宁愿避免通过低级ssh模块自己实现scp.
我知道paramiko
,一个支持ssh和sftp的Python模块; 但它不支持scp.
背景:我正在连接到不支持sftp但支持ssh/scp的路由器,所以sftp不是一个选项.
编辑:这是如何使用SCP或SSH将文件复制到Python中的远程服务器?. 但是,这个问题没有给出一个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')
Run Code Online (Sandbox Code Playgroud)
小智 93
试试模块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())
Run Code Online (Sandbox Code Playgroud)
然后调用scp.get()或scp.put()来执行scp操作.
Pat*_*otz 14
您可能对尝试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')
Run Code Online (Sandbox Code Playgroud)
如果你在win32上安装putty你得到一个pscp(putty scp).
所以你也可以在win32上使用os.system hack.
(并且您可以使用油灰剂进行密钥管理)
对不起它只是一个黑客(但你可以把它包装在一个python类)
找不到直接的答案,这个"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')
Run Code Online (Sandbox Code Playgroud)
截至今天,最好的解决方案可能是 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)
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
看一下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/')
Run Code Online (Sandbox Code Playgroud)
自从提出这个问题以来已经有一段时间了,与此同时,另一个可以处理这个问题的库已经出现:您可以使用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)
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用包子进程和命令调用来从外壳程序使用scp命令。
from subprocess import call
cmd = "scp user1@host1:files user2@host2:files"
call(cmd.split(" "))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
233502 次 |
最近记录: |