在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) client = paramiko.SSHClient()
stdin, stdout, stderr = client.exec_command(command)
Run Code Online (Sandbox Code Playgroud)
有没有办法获得命令返回码?
很难解析所有stdout/stderr并知道命令是否成功完成.
我正在使用Paramiko通过ssh连接到服务器.
基本身份验证运行良好,但我无法理解如何连接公钥.
当我用putty连接时,服务器告诉我这个:
Using username "root".
Authenticating with public key "rsa-key@ddddd.com"
Passphrase for key "rsa-key@ddddd.com": [i've inserted the passphrase here]
Last login: Mon Dec 5 09:25:18 2011 from ...
Run Code Online (Sandbox Code Playgroud)
我用这个ppk文件连接到它:
PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: rsa-key@dddd.com
Public-Lines: 4
[4 lines key]
Private-Lines: 8
[8 lines key]
Private-MAC: [hash]
Run Code Online (Sandbox Code Playgroud)
使用基本身份验证,我得到的错误(来自日志)是:
DEB [20111205-09:48:44.328] thr=1 paramiko.transport: userauth is OK
DEB [20111205-09:48:44.927] thr=1 paramiko.transport: Authentication type (password) not permitted.
DEB [20111205-09:48:44.927] thr=1 paramiko.transport: Allowed methods: ['publickey', 'gssapi-with-mic']
Run Code Online (Sandbox Code Playgroud)
我试图包含那个ppk文件并设置为auth_public_key,但是没有用.
你能帮助我吗?
如何在远程服务器上通过SSHClient进行SFTP传输?我有一个本地主机和两个远程主机.远程主机是备份服务器和Web服务器.我需要在备份服务器上找到必要的备份文件,并通过sftp将其放在Web服务器上.如何让paramiko的SFTP传输与paramiko的SSHClient一起工作?
最近,我根据paramiko制作了一个连接工作站的代码,其中包含不同的用户名(感谢私钥).
我从来没有遇到任何问题,但今天,我有: SSHException: Error reading SSH protocol banner
这很奇怪,因为它在任何连接上都是随机发生的.有没有办法解决它?
def exec_command(self, command, bufsize=-1):
#print "Executing Command: "+command
chan = self._transport.open_session()
chan.exec_command(command)
stdin = chan.makefile('wb', bufsize)
stdout = chan.makefile('rb', bufsize)
stderr = chan.makefile_stderr('rb', bufsize)
return stdin, stdout, stderr
Run Code Online (Sandbox Code Playgroud)
在paramiko中执行命令时,它总是在运行exec_command时重置会话.我希望能够执行sudo或su,并且当我运行另一个exec_command时仍然具有这些权限.另一个例子是尝试exec_command("cd /"),然后再次运行exec_command并让它在根目录中.我知道你可以做类似exec_command("cd /; ls -l")的东西,但我需要在单独的函数调用中完成它.
我发布了上述有关使用Paramiko收到的持久性错误消息的问题.我不认为这与我的下一个问题有关,但可能是.
我可以使用Paramiko通过SSH成功连接到我的服务器.我可以执行像ls或pwd这样的命令.我似乎无法做的是更改目录.我可以发送命令"cd ..",但是当我跟进"pwd"时,它表明我没有更改目录.它只列出了我登录时的初始目录.
>>> stdin, stdout, stderr = myssh.exec_command("pwd")
>>> stdout.readlines()
['/big/dom/home/myid\n']
>>> stdin, stdout, stderr = myssh.exec_command("cd ../")
>>> stdout.readlines()
[]
>>> stdin, stdout, stderr = myssh.exec_command("pwd")
>>> stdout.readlines()
['/big/dom/home/myid\n']
>>>
Run Code Online (Sandbox Code Playgroud)
我误解了这里发生了什么吗?我应该无法更改目录吗?或者如果我可以,我应该以除了使用exec_command之外的其他方式来做这件事吗?
-
再过7个小时我无法回答我自己的问题,所以答案是:
这个家伙弄清楚了:http://www.vertigrated.com/blog/2010/02/python-remote-ssh-with-paramiko/
您只需使用一个exec_command发送多个命令,例如:
myssh.exec_command('cd ..; pwd')
然后stdout.readlines()将返回您更改的目录.
我正试图通过paramiko运行一个交互式命令.cmd执行尝试提示输入密码但我不知道如何通过paramiko的exec_command提供密码并且执行挂起.如果cmd执行需要交互式输入,有没有办法将值发送到终端?
ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("psql -U factory -d factory -f /tmp/data.sql")
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?谢谢.
完整的工作测试案例
当然,根据您在本地和远程计算机上的内存,您的阵列大小会有所不同.
z1 = numpy.random.rand(300000000,2);
for i in range(1000):
print('*******************************************\n');
direct_output = subprocess.check_output('ssh blah@blah "ls /"', shell=True);
direct_output = 'a'*1200000;
a2 = direct_output*10;
print(len(direct_output));
Run Code Online (Sandbox Code Playgroud)
当前用例
如果它有助于我的用例如下:
我发出数据库查询,然后将结果表存储在远程计算机上.然后我想通过网络传输它们并进行分析.到目前为止,我在python中做了类似下面的事情:
#run a bunch of queries before hand with the results in remote files
....
counter = 0
mergedDataFrame = None
while NotDone:
output = subprocess.check_output('ssh blah@blah cat /data/file%08d'%(counter))
data = pandas.read_csv(...)
#do lots of analysis, append, merge, numpy stuff etc...
mergedDataFrame = pandas.merge(...)
counter += 1
Run Code Online (Sandbox Code Playgroud)
在某些时候,我在check_output命令中收到以下错误:[Errno 12]无法分配内存
背景
感谢以下问题,我想我已经知道出了什么问题.发布了许多解决方案,我试图确定哪些解决方案将避免[Errno 12]无法使用fork/clone分配与子进程实现相关的内存错误.
我在我的python代码中使用Paramiko(对于sftp).除了我每次导入或调用paramiko函数时,一切正常.此警告将显示:
C:\Python26\lib\site-packages\Crypto\Util\randpool.py:40: RandomPool_Deprecation
Warning: This application uses RandomPool, which is BROKEN in older releases. S
ee http://www.pycrypto.org/randpool-broken
RandomPool_DeprecationWarning)
Run Code Online (Sandbox Code Playgroud)
我知道这与Paramiko正在使用PyCrypto的一些不推荐的功能这一事实有关.
我的问题是,有没有办法以编程方式抑制此警告?我试过这个:
warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='paramiko')
Run Code Online (Sandbox Code Playgroud)
甚至这个:
warnings.filterwarnings(action='ignore', \
category=DeprecationWarning, module='randpool')
Run Code Online (Sandbox Code Playgroud)
在'import paramiko'语句之前和paramiko特定函数调用之前,但没有任何作用.无论如何,这个警告都会出现.如果有帮助,这是第三方库中打印警告的代码:
在randpool.py中:
from Crypto.pct_warnings import RandomPool_DeprecationWarning
import Crypto.Random
import warnings
class RandomPool:
"""Deprecated. Use Random.new() instead.
See http://www.pycrypto.org/randpool-broken
"""
def __init__(self, numbytes = 160, cipher=None, hash=None, file=None):
warnings.warn("This application uses RandomPool, which is BROKEN in older releases. See http://www.pycrypto.org/randpool-broken",
RandomPool_DeprecationWarning)
Run Code Online (Sandbox Code Playgroud)
如果您知道解决方法,请帮我关闭此警告.
paramiko ×10
python ×10
ssh ×5
memory ×1
networking ×1
public-key ×1
putty ×1
pycrypto ×1
scp ×1
sftp ×1
subprocess ×1
unix ×1