pysftp.CnOpts() 'No Host Keys Found' 错误,尽管使用 ssh-keyscan 生成了 known_hosts 文件 [英] pysftp.CnOpts() 'No Host Keys Found' error despite known_hosts file generated using ssh-keyscan
问题描述
我正在尝试遵循 使用 pysftp 验证主机密钥的解决方案.
我跑:
导入pysftpfn = r'C:\Users\UWAdmin\.ssh\known_hosts'cnopts = pysftp.CnOpts()cnopts.hostkeys.load(fn)
但我收到错误:
HostKeysException Traceback(最近一次调用)<ipython-input-3-b5b4d53fef6c>在<模块>---->9 cnopts = pysftp.CnOpts()10 cnopts.hostkeys.load(fn)~\miniconda3\envs\pycontrol\lib\site-packages\pysftp\__init__.py in __init__(self, knownhosts)62 其他:63 如果 len(self.hostkeys.items()) == 0:--->64 引发 HostKeysException('找不到主机密钥')6566 def get_hostkey(自我,主机):HostKeysException:找不到主机密钥
即使我做了 ssh-keyscan 192.168.254.254 >~/.ssh/known_hosts
在 Windows PowerShell 中,将以下内容添加到 ~/.ssh/known_hosts
文件:
并且,请注意,即使在 cnopts = pysftp.CnOpts()
我尝试重新安装 pysftp 仍然无济于事.请指教
当我通过 ssh 远程连接到服务器时,它还会提示我验证主机密钥,尽管它已经在 knbown_hosts
文件中.当我验证密钥时,添加到 C:\\Users\\UWAdmin/.ssh/known_hosts
的密钥是用类似中文的符号编写的:
㤱⸲㘱⸸㔲⸴㔲‴捥獤ⵡ桳㉡渭獩灴㔲‶䅁䅁㉅槩䡚桎塌潡佡桢獤佡佡桢佡佃佡佡佗䅁䥁浢穬䡤祁呎䅙䅁䉂䝂㍱敭䰳䵘畖匶愵灈䨷剱乍杒扁䕤䩳㑙䭐㑃摹㍓㡒䩵汫㑕橅䑒乑偎卷捗䍲焕䕃㕮杈䵉貘㘹ㅱ潚㥨乁㵙
按照 Martin 的建议尝试使用 paramiko.hostkeys.HostKeyEntry.from_line
解析 known_hosts
的各个行,我在 Paramiko 日志中得到了这个:
INF [20201104-16:36:28.943] thr=1 paramiko.hostkeys:无法处理 s s h - r s a 类型的键INF [20201104-16:36:28.943] thr=1 paramiko.hostkeys:在第 0 行的 known_hosts 中找不到足够的字段('\x00\n')INF [20201104-16:36:28.943] thr=1 paramiko.hostkeys:无法处理类型 e c d s a - s h a 2 - n i s t p 2 5 6INF [20201104-16:36:28.943] thr=1 paramiko.hostkeys:在第 0 行的 known_hosts 中找不到足够的字段('\x00\n')INF [20201104-16:36:28.943] thr=1 paramiko.hostkeys:无法处理 s s h - e d 2 5 5 1 9 类型的密钥INF [20201104-16:36:28.943] thr=1 paramiko.hostkeys:在第 0 行的 known_hosts 中找不到足够的字段('\x00\n')
正如您在调用堆栈中看到的,您在 CnOpts
构造函数中已经收到错误消息,甚至在您调用 cnopts 之前.hostkeys.load
.这是因为构造函数试图从标准位置加载主机密钥文件.
这应该可以用于指定您的自定义位置:
cnopts = pysftp.CnOpts(knownhosts=fn)
尽管您实际上是从标准位置加载密钥,但问题一定出在文件格式上.从您提供的附加信息来看,该文件似乎采用 UTF-16 编码.将其另存为 ASCII/UTF-8.
这实际上是从 PowerShell 调用 ssh-keyscan
的结果.在处理文件重定向时,版本 6 之前的 PowerShell 默认为 UTF-16.请参阅 为什么 PowerShell 重定向 >>更改文本内容的格式? 只需在 cmd.exe
中运行 ssh-keyscan
而不是 PowerShell.
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')
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.
这篇关于pysftp.CnOpts() 'No Host Keys Found' 错误,尽管使用 ssh-keyscan 生成了 known_hosts 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!