pysftp.CnOpts() 'No Host Keys Found' error despite known_hosts file generated using ssh-keyscan

Hud

I am trying to follow solution from Verify host key with pysftp.

I run:

import pysftp
fn = r'C:\Users\UWAdmin\.ssh\known_hosts'

cnopts = pysftp.CnOpts()
cnopts.hostkeys.load(fn)

but I get the error:

HostKeysException                         Traceback (most recent call last)
<ipython-input-3-b5b4d53fef6c> in <module>
----> 9 cnopts = pysftp.CnOpts()
     10 cnopts.hostkeys.load(fn)

~\miniconda3\envs\pycontrol\lib\site-packages\pysftp\__init__.py in __init__(self, knownhosts)
     62         else:
     63             if len(self.hostkeys.items()) == 0:
---> 64                 raise HostKeysException('No Host Keys Found')
     65 
     66     def get_hostkey(self, host):

HostKeysException: No Host Keys Found

Even after I did ssh-keyscan 192.168.254.254 > ~/.ssh/known_hosts in Windows PowerShell, what added the following to the ~/.ssh/known_hosts file:

192.168.254.254 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCVlnFrb1SzjijeWRld0w+MJpblrsF8vEutsRnJbxOMHKz8dhqP/qGjYOtG3KCLwNH8odLStd5or5C68XqbdBTxXG1CaTrSd0Z4gWo3cNy3rKjJ4pmTVPuFXEH7iCfd9GNDfPtUOZDeJhbAXID8mUXtnGaw4jH3veWSmLGQk/sbNRgFfVytAqhGxn8wVgBmVt5VGmaQN9f35mikfmyRZtwQXwZ/sbvNYYiGVbd0mnztawAdv9CZhtdJBofj1yqldw/yfN7m/8AkKHqAOlRfbKMIXU+VXkKTwg+try/aYA76HJPmS5jU/C3esc/2wyZBP7t9fMOF6iUbimCsHCC2MP3P
192.168.254.254 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGq3me3LXMVu6S5aHp7JqRMNRgAbdEsJY4PKC4ydS3R8uJklU4EjRDQNNPwSWcrCeqCEn5HgIMOs96q1Zoh9ANY=
192.168.254.254 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAgEX0CF1NzUL0G0+Wf64qzJmj5PBh6JI95Xn5xaS5y6

And, notice that no host keys are found even at cnopts = pysftp.CnOpts()

I have tried reinstalling pysftp still to no avail. Please advise

When I ssh to remote to server and it also prompts me to verify the host key, despite it being on the knbown_hosts file already. When I verify the key, the key added to the C:\\Users\\UWAdmin/.ssh/known_hosts is written in chinese-like symbols:

㤱⸲㘱⸸㔲⸴㔲‴捥獤ⵡ桳㉡渭獩灴㔲‶䅁䅁㉅橖䡚桎塌潎呙瑉浢穬䡤祁呎䅙䅁䥁浢穬䡤祁呎䅙䅁䉂䝂㍱敭䰳䵘畖匶愵灈䨷剱乍杒扁䕤䩳㑙䭐㑃摹㍓㡒䩵汫㑕橅䑒乑偎卷捗䍲煥䕃㕮杈䵉獏㘹ㅱ潚㥨乁㵙਍

Following the suggestion from Martin to try to parse individual lines of known_hosts using paramiko.hostkeys.HostKeyEntry.from_line, I get this in Paramiko log:

INF [20201104-16:36:28.943] thr=1   paramiko.hostkeys: Unable to handle key of type  s s h - r s a 
INF [20201104-16:36:28.943] thr=1   paramiko.hostkeys: Not enough fields found in known_hosts in line 0 ('\x00\n')
INF [20201104-16:36:28.943] thr=1   paramiko.hostkeys: Unable to handle key of type  e c d s a - s h a 2 - n i s t p 2 5 6 
INF [20201104-16:36:28.943] thr=1   paramiko.hostkeys: Not enough fields found in known_hosts in line 0 ('\x00\n')
INF [20201104-16:36:28.943] thr=1   paramiko.hostkeys: Unable to handle key of type  s s h - e d 2 5 5 1 9 
INF [20201104-16:36:28.943] thr=1   paramiko.hostkeys: Not enough fields found in known_hosts in line 0 ('\x00\n')
Martin Prikryl

As you can see in the callstack, you get the error in CnOpts constructor already, even before you call cnopts.hostkeys.load. That's because the constructor tries to load the host keys file from the standard location.

This should work to specify your custom location:

cnopts = pysftp.CnOpts(knownhosts=fn)

Though as you are actually loading the key from the standard location, the problem must be in the file format. From the additional information, you have provided, it looks like the file is in UTF-16 encoding. Save it as ASCII/UTF-8 instead.

It is actually a consequence of calling ssh-keyscan from the PowerShell. PowerShell before version 6 defaults to UTF-16, when processing file redirection. See Why does PowerShell redirection >> change the formatting of the text content? Just run the ssh-keyscan in cmd.exe instead of PowerShell.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Datadog ssh integration error: Server 'localhost' not found in known_hosts

SSH add key to known_hosts from ssh-keyscan

pysftp throwing paramiko.ssh_exception.SSHException even though known_hosts file provided?

MySQL\Workbench\ssh\known_hosts not found

ssh/known_hosts: No such file or directory

Using SSH to connect to a new server without storing the host keys in the $HOME/.ssh/known_host file

known_hosts file on HCI giving Error: Host Key has been changed

Spring SFTP Integration - Cannot Connect To Host Using known_hosts File

Verify host key with pysftp which has been already added to known_hosts

ssh-keyscan not found in Dockerfile

Add host to known_hosts file without prompt

Existing host keys found error when ssh in gcp vm instance

Windows 10 SSH folder and known_hosts file is missing

Why doesn't "ssh-keygen -R" remove the host from ~/.ssh/known_hosts

Still getting “ssh failure: Offending RSA key in /var/lib/sss/pubconf/known_hosts” despite removing the specified key from “known_hosts”

Bash: SSH: Checking if an IP or host with non standard port is in known_hosts

SSH key-based authentication: known_hosts vs authorized_keys

How to clear duplicated entries in SSH known_hosts and authorized_keys files?

SSH error on Linux : Failed to add the host to the list of known hosts

Paramiko SSH failing with "Server '...' not found in known_hosts" when run on web server

"No hostkey for ... found" in pysftp code even though cnopts.hostkeys is set to None

Connecting to multiple SFTP sites using pysftp in a loop: "AttributeError: 'Connection' object has no attribute 'CnOpts'"

Identify the entries within .ssh/known_hosts?

ansible ssh prompt known_hosts issue

known_hosts not updating, but ssh says it added

Avoiding duplicate entries in ~/.ssh/known_hosts

Generate hashed name for SSH known_hosts

Differing SSH known_hosts formats

SSH client seems to be checking '/dev/null' instead of 'known_hosts' file when checking remote server's ECDSA key