“找不到主机*****的主机密钥";使用私钥通过pysftp连接到SFTP服务器时 [英] "No hostkey for host ***** found" when connecting to SFTP server with pysftp using private key

查看:150
本文介绍了“找不到主机*****的主机密钥";使用私钥通过pysftp连接到SFTP服务器时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此我在通过SFTP连接到远程服务器时遇到很多问题.我已经尝试过像下面这样的正常方式.

So I am having many issues connecting to a remote server via SFTP. I have tried the normal way like below.

sftp = pysftp.Connection(host='Host',username='username',password='passwd',private_key=".ppk")

哪个没有用.我收到以下错误:

Which did not work. I got the following error:

SSHException:找不到主机*****的主机密钥.

SSHException: No hostkey for host ***** found.

然后我尝试了以下操作:

I then tried the following:

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
s = pysftp.Connection(host='host', username='user', password='password', cnopts=cnopts)

这也不起作用.我收到以下错误:

Which also did not work. I got the following error:

BadAuthenticationType :(错误身份验证类型",['publickey'])(allowed_types = ['publickey'])

BadAuthenticationType: ('Bad authentication type', ['publickey']) (allowed_types=['publickey'])

当我运行以下命令时:

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("host",username = "username",password = "password")
ssh_session = client.get_transport().open_session()

我遇到相同的错误:

BadAuthenticationType :(错误身份验证类型",['publickey'])(allowed_types = ['publickey'])

BadAuthenticationType: ('Bad authentication type', ['publickey']) (allowed_types=['publickey'])

推荐答案

您正在混淆用于身份验证的私钥和用于验证服务器标识的主机密钥.两者都需要照顾,而您所有的代码尝试仅照顾其中之一.请参阅 SSH密钥对上的 my 文章,以了解两者之间的区别SSH涉及两种密钥.

Your are confusing a private key used for authentication and a host key used to verify an identify of a server. Both need to be taken care of, while all your code attempts take care of one of them only. See my article on SSH key pairs to understand the difference between the two kinds of keys involved in SSH.

所以这应该起作用":

# Accept any host key (still wrong see below)
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# And authenticate with private key
sftp = pysftp.Connection(
  host='Host', username='username', password='passwd', private_key=".ppk", cnopts=cnopts)

但是此代码实际上将盲目接受任何主机密钥(cnopts.hostkeys = None),这是一个安全漏洞.
有关正确的方法,请参见使用pysftp验证主机密钥.

But this code will actually blindly accept any host key (cnopts.hostkeys = None), what is a security flaw.
For a correct approach, see Verify host key with pysftp.

这篇关于“找不到主机*****的主机密钥";使用私钥通过pysftp连接到SFTP服务器时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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