[英]Can not connect via AsyncSSH, error Host key is not trusted
当我运行这个脚本时,我收到SSH connection failed: Host key is not trusted
错误,但即使连接到此主机以获取密钥,也会收到此错误。
import asyncio, asyncssh, sys
async def run_client():
async with asyncssh.connect('172.18.17.9', username="user", password="admin", port=9321) as conn:
result = await conn.run('display version', check=True)
print(result.stdout, end='')
try:
asyncio.get_event_loop().run_until_complete(run_client())
except (OSError, asyncssh.Error) as exc:
sys.exit('SSH connection failed: ' + str(exc))
尝试将known_hosts=None
参数添加到连接方法。
asyncssh.connect('172.18.17.9', username="user", password="admin", port=9321, known_hosts=None)
来自此处的 asyncssh 文档: https://asyncssh.readthedocs.io/en/latest/api.html#asyncssh.SSHClientConnectionOptions
known_hosts (请参阅指定已知主机)–(可选)将用于验证 SSH 握手期间提供的服务器主机密钥的密钥列表。 如果未指定,则将在 file.ssh/known_hosts 中查找密钥。 如果明确设置为 None,服务器主机密钥验证将被禁用。
这是相关的,但可能不完全是你的救恩:
https://github.com/ronf/asyncssh/issues/132
当你问这个问题(帮助我们帮助你)时,你应该问自己的真正问题是它在哪里失败了? 类比的已知主机就像环境变量一样,在您需要它们时不会出现。
编辑:立即引发的问题。 找到主机密钥但不受信任? 唔?
EDIT2:不想对你苛刻,但我认为这是一个有用的纠正。 您有一个可以找到密钥但不知道的软件库。 你会遇到很多 SSH / shell / env var 的情况,你认为理所当然的事情是未知的。 清楚地思考以帮助自己并更好地提出问题。
对我来说,插入known_hosts=None
后运行流畅
这是我在尝试 Ortega 书中的编码示例时的示例:我尝试使用 localCentOS 的hostname=ip/username/password
,命令 test 是 ifconfig
import asyncssh
import asyncio
import getpass
async def execute_command(hostname, command, username, password):
async with asyncssh.connect(hostname, username = username,password=password,known_hosts=None) as connection:
result = await connection.run(command)
return result.stdout
您应该始终验证服务器的公钥。
根据您的用例,您可以:
~/.ssh/known_hosts
并且 AsyncSSH 将使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.