【问题标题】:Download files from FTP server: Exception: Error reading SSH protocol banner从 FTP 服务器下载文件:异常:读取 SSH 协议横幅时出错
【发布时间】:2017-06-23 16:06:21
【问题描述】:

我想使用以下代码从 FTP 服务器下载文件(我在 Linux 14.04 lts、Python 版本 2.7.13、Paramiko 版本 2.2.1 上安装了一个测试 vsftpd 服务器)(我没有发布全部, 仅在引发异常的情况下)

import datetime
import socket
import paramiko
import os
import shutil

today = datetime.date.today() - datetime.timedelta(days=3)
formattedtime = today.strftime('%Y%m%d')
destination = '/home/path/TestDir-%s' % formattedtime

if not os.path.exists(destination):
   os.mkdir(destination)

def file_download(hostname, username, hostport, password):
    rsa_private_key = r"~/.ssh/id_rsa"
    def agent_auth(transport, username):

    try:
        ki = paramiko.RSAKey.from_private_key_file(rsa_private_key)
    except Exception, e:
        print 'Failed loading {} {}'.format(rsa_private_key, e)        
    agent = paramiko.Agent()
    agent_keys = agent.get_keys() + (ki,)
    if len(agent_keys) == 0:
        return
    for key in agent_keys:
        print 'Trying ssh-agent key{}'.format(key.get_fingerprint().encode('hex'), )
        try:
            transport.auth_publickey(username, key)
            print '... success!'
            return
        except paramiko.SSHException, e:
            print '... failed!', e
    password = password  # This is used when password is used to login
    host = hostname
    username = username
    port = hostport
    paramiko.util.log_to_file("/home/path/Desktop//filename.log")
    hostkeytype = None
    hostkey = None
    files_copied = 0
    try:
        host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
    except IOError:
        try:
            host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts'))
        except IOError:
            print '*** Unable to open host keys file'
            host_keys = {}
    if hostname in host_keys:
        hostkeytype = host_keys[hostname].keys()[0]
        hostkey = host_keys[hostname][hostkeytype]
        print 'Using host key of type %s' % hostkeytype
    try:
        transport = paramiko.Transport((host, port))
        transport.start_client()
        agent_auth(transport, username)
        if not transport.is_authenticated():
            print 'RSA key auth failed! Trying password login...'
            transport.auth_password(username=username, password=password)
        else:
            ssftp = transport.open_session()
        ssftp = paramiko.SFTPClient.from_transport(transport)
        print ssftp
    except Exception as qw:
        print "asdasd {}".format(qw)

但我总是得到这个例外:

读取 SSH 协议横幅时出错

这是堆栈跟踪:

DEB [20170623-17:28:22.595] thr=1  paramiko.transport: starting thread (client mode): 0x2806c910L DEB [20170623-17:28:22.595] thr=1 paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.1.2 
DEB [20170623-17:28:22.596] thr=1  paramiko.transport: Banner: 220 (vsFTPd 3.0.2) DEB [20170623-17:28:22.596] thr=1   paramiko.transport: Banner: 530 Please login with USER and PASS. 
ERR [20170623-17:28:24.599] thr=1  paramiko.transport: Exception: Error reading SSH protocol banner ERR [20170623-17:28:24.600] thr=1 paramiko.transport: Traceback (most recent call last): 
ERR [20170623-17:28:24.600] thr=1  paramiko.transport: File "/balh/blah/anaconda2/lib/python2.7/site-packages/paramiko/transport.py", line 1749, in run ERR [20170623-17:28:24.600] thr=1  paramiko.transport:     self._check_banner() 
ERR [20170623-17:28:24.600] thr=1  paramiko.transport: File "/balh/blah/anaconda2/lib/python2.7/site-packages/paramiko/transport.py", line 1897, in _check_banner 
ERR [20170623-17:28:24.600] thr=1  paramiko.transport: raise SSHException('Error reading SSH protocol banner' + str(e)) 
ERR [20170623-17:28:24.600] thr=1  paramiko.transport: SSHException: Error reading SSH protocol banner

我已经尝试在transport.py 中增加self.banner_timeout = 60,就像某些票证中建议的那样,但没有成功。

【问题讨论】:

  • 请修正您帖子中的代码格式
  • 谢谢。完毕!现在一定更好

标签: python ssh download ftp paramiko


【解决方案1】:

横幅:220 (vsFTPd 3.0.2) ...

这意味着您正在连接到 FTP 服务器。

SSHException: 读取 SSH 协议横幅时出错

这意味着您期望的是 SSH 服务器而不是 FTP 服务器。

造成这种混淆的原因是您假设 SFTP 就像 FTP,但事实并非如此。这些是完全不同的协议。 SFTP 是基于 SSH 的文件传输,而 FTP 是 RFC959 中描述的 30 多年前的协议。并且 FTPS(不是 SFTP)是添加到这个旧协议的 SSL 支持。

要访问 FTP 或 FTPS 服务器,您可以在 Python 中使用 ftplib
要使用 SFTP 访问您的服务器,请使用端口 22 (ssh) 而不是端口 21 (ftp) 作为目标端口,前提是该端口上有一个 SSH 服务器也允许 SFTP。

【讨论】:

  • 非常感谢您的回答。我已经测试了ftplib,然后拼命寻找替代方案,直到我找到paramiko...就像你说的那样,这不是我想的那样。这张票很好地描述了我在 ftplib 中遇到的问题。因此,如果您有时间,请随时提出可能的解决方案/替代方案。 Deal with EOFError while downloading files from server
  • @sdikby:我怀疑您在票证中描述的问题与 ftplib 有关,但它很可能是服务器过载、服务器限制或类似情况。可悲的是,您从未跟进那里的 cmets 询问服务器端日志以及独立 FTP 客户端是否有类似问题。
猜你喜欢
  • 2014-10-25
  • 1970-01-01
  • 2011-11-04
  • 2017-10-05
  • 1970-01-01
  • 2020-10-08
  • 2022-08-07
  • 2022-12-10
  • 1970-01-01
相关资源
最近更新 更多