如何修复 Paramiko SSHException: Server ... not found in known_hosts
问题:
尝试使用 paramiko 通过类似以下代码连接到 SSH 服务器时
paramiko_known_hosts.txt
ssh = paramiko.SSHClient()
ssh.connect("192.168.1.112")你看到类似这样的错误消息
paramiko_sshexception.txt
SSHException: Server '192.168.1.112' not found in known_hosts解决方案
最简单的解决方案是添加
paramiko_autoaddpolicy.py
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())在调用 connect() 之前:
paramiko_connect_example.txt
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.112")这将自动将未知主机密钥添加到已知主机存储中。注意这在某种程度上破坏了验证主机密钥的目的,如果可能你应该手动验证主机密钥。但是,在许多应用中,没有人可以在自动化过程中验证主机密钥,因此此解决方案更实用。只需记住其安全影响。
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow