这是我使用paramiko的代码:

#!/usr/bin/env python   
from paramiko.client import SSHClient   
from paramiko.client import AutoAddPolicy   
import logging; logging.basicConfig(); logging.getLogger('paramiko.transport').setLevel(logging.DEBUG) 
 
class MySFTP(object):   
    def __init__(self, hostname, port, username, password): 
        self.client = SSHClient() 
        self.client.set_missing_host_key_policy(AutoAddPolicy) 
        self.client.connect(hostname=hostname, port=port, username=username, password=password)   
        self.ssh_channel = self.client.invoke_shell()   
        self.sftp_session = self.client.open_sftp() 
 
    def get_file_from_target(self,remotepath_file, localpath_file): 
        self.sftp_session.get(remotepath=remotepath_file, localpath=localpath_file) 
 
    def end_session(self): 
        self.sftp_session.close() 
        self.client.close() 
 
if `__name__` == `'__main__'`: 
    sftp_object = MySFTP(hostname="192.168.21.2", port=22, username="root", password="root") 
    sftp_object.get_file_from_target(localpath_file = r"/home/admin1/folder_to_get/file_to_get", remotepath_file = r"/home/root/folder_to_get/file_to_get") 
    print("File transferred successfully.") 
    sftp_object.end_session() 

出于某种原因,当我尝试从一台Linux计算机上获取文件时,此方法可以完美地工作,但是当尝试从目标系统中获取文件时,该方法将失败。我已经验证了在bash中尝试时可以通过ssh在两台计算机上执行命令。我还可以使用scp从两台计算机上获取文件。但是,在此代码中,使用以下日志从目标系统中获取文件时, self.sftp_session = self.client.open_sftp()行失败:
DEBUG:paramiko.transport:starting thread (client mode): 0x1b21c0d0L   
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.4.0   
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-dropbear_2016.72   
INFO:paramiko.transport:Connected (version 2.0, client dropbear_2016.72)   
DEBUG:paramiko.transport:kex algos:[u'curve25519-sha256@libssh.org', u'ecdh-sha2-nistp521', u'ecdh-sha2-nistp384', u'ecdh-sha2-nistp256', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1', u'kexguess2@matt.ucc.asn.au'] server key:[u'ssh-rsa'] client encrypt:[u'aes128-ctr', u'aes256-ctr', u'aes128-cbc', u'aes256-cbc', u'twofish256-cbc', u'twofish-cbc', u'twofish128-cbc', u'3des-ctr', u'3des-cbc'] server encrypt:[u'aes128-ctr', u'aes256-ctr', u'aes128-cbc', u'aes256-cbc', u'twofish256-cbc', u'twofish-cbc', u'twofish128-cbc', u'3des-ctr', u'3des-cbc'] client mac:[u'hmac-sha1-96', u'hmac-sha1', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-md5'] server mac:[u'hmac-sha1-96', u'hmac-sha1', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-md5'] client compress:[u'zlib@openssh.com', u'none'] server compress:[u'zlib@openssh.com', u'none'] client lang:[u''] server lang:[u''] kex follows?False   
DEBUG:paramiko.transport:Kex agreed: ecdh-sha2-nistp256   
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa   
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr   
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256   
DEBUG:paramiko.transport:Compression agreed: none   
DEBUG:paramiko.transport:kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>   
DEBUG:paramiko.transport:Switch to new keys ...   
DEBUG:paramiko.transport:Adding ssh-rsa host key for 192.168.21.2: f5b6a7b3fd5537ea9bd5085b49cbca7a   
DEBUG:paramiko.transport:userauth is OK   
INFO:paramiko.transport:Authentication (password) successful!   
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes   
DEBUG:paramiko.transport:[chan 0] Max packet out: 32759 bytes   
DEBUG:paramiko.transport:Secsh channel 0 opened.   
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok   
DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok   
DEBUG:paramiko.transport:[chan 1] Max packet in: 32768 bytes   
DEBUG:paramiko.transport:[chan 1] Max packet out: 32759 bytes   
DEBUG:paramiko.transport:Secsh channel 1 opened.   
DEBUG:paramiko.transport:[chan 1] Sesch channel 1 request ok   
DEBUG:paramiko.transport:[chan 1] EOF received (1)   
Traceback (most recent call last):   
  File "./sftp_remote_so.py", line 26, in <module>   
    sftp_object = MySFTP(hostname="192.168.21.2", port=22, username="root", password="root")   
  File "./sftp_remote_so.py", line 15, in __init__   
    self.sftp_session = self.client.open_sftp()   
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 521, in open_sftp   
    return self._transport.open_sftp_client()   
  File "/usr/local/lib/python2.7/dist-packages/paramiko/transport.py", line 980, in open_sftp_client   
    return SFTPClient.from_transport(self)   
  File "/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 140, in from_transport   
    return cls(chan)   
  File "/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 105, in __init__   
    raise SSHException('EOF during negotiation')   
paramiko.ssh_exception.SSHException: EOF during negotiation   

请您参考如下方法:

注意到您的远程SSH服务器是dropbear,并且我有一个OpenWRT wifi路由器(与SSH服务器具有dropbear),所以我尝试了一下,并得到了同样的错误。

Traceback (most recent call last): 
  File "foo.py", line 22, in <module> 
    sftp_object = MySFTP(hostname="wifi", port=22, username="root", password="...") 
  File "foo.py", line 12, in __init__ 
    self.sftp_session = self.client.open_sftp() 
  File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 452, in open_sftp 
    return self._transport.open_sftp_client() 
  File "/usr/lib/python2.7/dist-packages/paramiko/transport.py", line 906, in open_sftp_client 
    return SFTPClient.from_transport(self) 
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 132, in from_transport 
    return cls(chan) 
  File "/usr/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 101, in __init__ 
    raise SSHException('EOF during negotiation') 
paramiko.ssh_exception.SSHException: EOF during negotiation 

然后我尝试了手动 sftp到我的wifi路由器:

[STEP 101] # sftp wifi 
root@wifi's password: 
ash: /usr/libexec/sftp-server: not found 
Connection closed 
[STEP 102] # 

因此,尽管SCP正常工作,但 dropbear似乎不支持SFTP。

更新:

在Debian 9上, dbclient( dropbear客户端)手册页显示:

[...] Dropbear doesn't implement sftp itself but the OpenSSH sftp client can be used [...]


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!