首页 > 解决方案 > 无法通过 AsyncSSH 连接,错误主机密钥不受信任

问题描述

当我运行此脚本时,我收到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))

标签: pythonsshasyncssh

解决方案


尝试将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 握手期间提供的服务器主机密钥的密钥列表。如果未指定,则将在文件 .ssh/known_hosts 中查找密钥。如果明确设置为 None,服务器主机密钥验证将被禁用。


推荐阅读