我正在尝试使用 pysftp 遵循验证主机密钥的解决方案。
我跑:
import pysftp
fn = r'C:\Users\UWAdmin\.ssh\known_hosts'
cnopts = pysftp.CnOpts()
cnopts.hostkeys.load(fn)
但我得到了错误:
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
即使在我ssh-keyscan 192.168.254.254 > ~/.ssh/known_hosts
使用 Windows PowerShell 之后,文件中也添加了以下内容~/.ssh/known_hosts
:
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
并且,请注意,即使在cnopts = pysftp.CnOpts()
我尝试重新安装 pysftp 仍然无济于事。请指教
当我通过 ssh 远程连接到服务器时,它还会提示我验证主机密钥,尽管它已经在knbown_hosts
文件中。当我验证密钥时,添加到的密钥C:\\Users\\UWAdmin/.ssh/known_hosts
是用类似中文的符号编写的:
㤱⸲㘱⸸㔲⸴㔲‴捥獤ⵡ桳㉡渭獩灴㔲‶䅁䅁㉅橖䡚桎塌潎呙瑉浢穬䡤祁呎䅙䅁䥁浢穬䡤祁呎䅙䅁䉂䝂㍱敭䰳䵘畖匶愵灈䨷剱乍杒扁䕤䩳㑙䭐㑃摹㍓㡒䩵汫㑕橅䑒乑偎卷捗䍲煥䕃㕮杈䵉獏㘹ㅱ潚㥨乁㵙
按照 Martin 的建议尝试解析known_hosts
using的各个行paramiko.hostkeys.HostKeyEntry.from_line
,我在 Paramiko 日志中得到了这个:
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')