从FTP服务器下载文件:异常:错误读取SSH协议横幅



我想从FTP服务器下载文件(我已经在Linux 14.04 LTS上安装了测试VSFTPD服务器,Python版本2.7.13,Paramiko版本2.2.1(使用以下代码(我不是发布所有内容,仅在提出例外的情况下(

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,就像某些门票中的建议一样,但没有成功。

横幅:220(VSFTPD 3.0.2(...

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

sshexception:错误读取SSH协议横幅

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

造成这种困惑的原因是您假设SFTP就像FTP一样,但事实并非如此。这些是完全不同的协议。SFTP是在SSH顶部的文件传输,而FTP是RFC959中描述的30多年历史协议。和FTP(不是SFTP(是SSL支持添加到此旧协议中。

要访问FTP或FTPS服务器,您可以在Python中使用ftplib
要使用SFTP访问服务器22(SSH(而不是端口21(FTP(作为目标端口,前提

最新更新