“No hostkey for host ***** found” when connecting to SFTP server with pysftp using private key

我的未来我决定 提交于 2020-07-15 05:43:11

问题


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: 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: ('Bad authentication type', ['publickey']) (allowed_types=['publickey'])

Also when I run the following:

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()

I get the same error:

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


回答1:


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.

So this should "work":

# 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)

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.



来源:https://stackoverflow.com/questions/53864260/no-hostkey-for-host-found-when-connecting-to-sftp-server-with-pysftp-usi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!