使用python pysftp软件包,获取"SSHException:来自服务器的错误主机密钥".错误 [英] Using python pysftp package, getting a "SSHException: Bad host key from server" error

查看:267
本文介绍了使用python pysftp软件包,获取"SSHException:来自服务器的错误主机密钥".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用0.2.9版的pysftp软件包运行Python 3.

I am running Python 3 with the pysftp package, version 0.2.9.

下面的代码在这里.我正在正确加载主机密钥,如行cnopts.hostkeys.keys()所示.

The following code I have is here. I am loading the hostkey correctly as shown by the line cnopts.hostkeys.keys().

import pysftp

key_file_test = './path_to_key_file/key_file.pub'

DOWNLOAD_UAT = {
    "USERNAME": "xxxxxxxx",
    "PASSWORD": "xxxxxxxx"
}

UAT_FTP_SITE = 'sftp-test.site.com'

cnopts = pysftp.CnOpts()
cnopts.hostkeys.load(key_file_test)
cnopts.hostkeys.keys()

'''['github.com', 'XXX.XX.XXX.XXX', 'sftp-test.site.com']'''

srv = pysftp.Connection(host=UAT_SFTP_SITE, username=DOWNLOAD_UAT['USERNAME'], 
                        password=DOWNLOAD_UAT['PASSWORD'], cnopts=cnopts, port=22)

然后我在运行最后一行时的错误是

Then my error when I run the last line is

---------------------------------------------------------------------------
SSHException                              Traceback (most recent call last)
<ipython-input-82-308ec955a92e> in <module>()
      8 
      9 srv = pysftp.Connection(host=UAT_SFTP_SITE, username=DOWNLOAD_UAT['USERNAME'], 
---> 10                         password=DOWNLOAD_UAT['PASSWORD'], cnopts=cnopts, port=22)
     11 data = srv.listdir()

C:\ProgramData\Anaconda3\lib\site-packages\pysftp\__init__.py in __init__(self, host, username, private_key, password, port, private_key_pass, ciphers, log, cnopts, default_path)
    141         self._transport.use_compression(self._cnopts.compression)
    142         self._set_authentication(password, private_key, private_key_pass)
--> 143         self._transport.connect(**self._tconnect)
    144 
    145     def _set_authentication(self, password, private_key, private_key_pass):

C:\ProgramData\Anaconda3\lib\site-packages\paramiko\transport.py in connect(self, hostkey, username, password, pkey, gss_host, gss_auth, gss_kex, gss_deleg_creds)
   1139                     key.get_name(), repr(key.asbytes()))
   1140                 )
-> 1141                 raise SSHException('Bad host key from server')
   1142             self._log(DEBUG, 'Host key verified (%s)' % hostkey.get_name())
   1143 

SSHException: Bad host key from server

有人知道这是什么问题吗?

Does anyone know what the problem is here?

推荐答案

这似乎是pysftp中的一个已知错误.在此处接受的答案中:

It looks like this is a known bug in pysftp. In the accepted answer here:

pysftp-paramiko SSHException,来自服务器的错误主机密钥

有直接使用pysftp包装的父库(paramiko)的示例代码:

There is example code that directly uses the parent library (paramiko) that pysftp wraps:

import paramiko
transport = paramiko.Transport(('server.com',22))
transport.connect(username='XXXXX', password='XXXXX')
sftp = paramiko.SFTPClient.from_transport(transport)
print(sftp.listdir())

我将上面的示例更新为不使用私钥进行连接,而是使用用户名/密码.

I updated the example above to not use a private key to connect, instead to use a username/password.

我刚刚测试的这段代码默认情况下将从~/.ssh/known_hosts加载所有公共密钥.如果您对该文件具有写访问权,那么这可能是最简单的解决方案.但是,如果不能,则Transport类具有add_server_key(key)函数,该函数将添加服务器标识.参见doc 此处.

This code I just tested will by default load all public keys from ~/.ssh/known_hosts. If you have write access to that file, that might be the easiest solution for you. But if you cannot, the Transport class has an add_server_key(key) function that would add the server identity. See doc here.

这篇关于使用python pysftp软件包,获取"SSHException:来自服务器的错误主机密钥".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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