从FTP服务器下载文件:异常:读取SSH协议标题时出错 [英] Download files from FTP server: Exception: Error reading SSH protocol banner

查看:240
本文介绍了从FTP服务器下载文件:异常:读取SSH协议标题时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to download files from an FTP server (I have installed a test vsftpd server on Linux 14.04 lts, Python version 2.7.13, Paramiko version 2.2.1) using the following code (I'm not posting all of it, only where the exception is raised)

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)

但我总是遇到以下异常:

But i get always this exception:


读取SSH协议标语时出错

Error reading SSH protocol banner

这是堆栈跟踪:

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

我已经尝试增加 self.banner_timeout = 60 transport.py ,如某些提示中所示键盘但未成功。

I have already tried to increase the self.banner_timeout = 60 in transport.py, like suggested in some tickets but without success.

推荐答案


横幅:220(vsFTPd 3.0.2)...

Banner: 220 (vsFTPd 3.0.2) ...

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

This means you are connecting to an FTP server.


SSHException:读取SSH协议标语时出错

SSHException: Error reading SSH protocol banner

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

This means you are expecting an SSH server not an FTP server.

造成这种混乱的原因是,您假设SFTP就像FTP,但事实并非如此。这些是完全不同的协议。 SFTP是基于SSH的文件传输,而FTP是 RFC959 。 FTPS(不是SFTP)是对旧协议的SSL支持。

The reason for this confusion is that you are assuming that SFTP is just like FTP, but its not. These are completely different protocols. SFTP is file transfer on top of SSH while FTP is the 30+ year old protocol described in RFC959. And FTPS (not SFTP) is SSL support added to this old protocol.

要访问FTP或FTPS服务器,可以使用 ftplib 在Python中。

要使用SFTP访问服务器,请使用端口22(ssh)而不是端口21(ftp)作为目标端口,但前提是此端口上还有一个SSH服务器也允许SFTP

To access an FTP or FTPS server you can use ftplib in Python.
To access your server with SFTP use port 22 (ssh) not port 21 (ftp) as target port, provided that there is an SSH server at this port which also allows SFTP.

这篇关于从FTP服务器下载文件:异常:读取SSH协议标题时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆