简体   繁体   English

无法通过 AsyncSSH 连接,错误主机密钥不受信任

[英]Can not connect via AsyncSSH, error Host key is not trusted

When I run this script I receive SSH connection failed: Host key is not trusted error, but even connect to this host to take the key, keep to receive this error.当我运行这个脚本时,我收到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))

Try adding the known_hosts=None parameter to the connect method.尝试将known_hosts=None参数添加到连接方法。

asyncssh.connect('172.18.17.9', username="user", password="admin", port=9321, known_hosts=None)

From asyncssh documentation here: https://asyncssh.readthedocs.io/en/latest/api.html#asyncssh.SSHClientConnectionOptions来自此处的 asyncssh 文档: https://asyncssh.readthedocs.io/en/latest/api.html#asyncssh.SSHClientConnectionOptions

known_hosts (see Specifying known hosts) – (optional) The list of keys which will be used to validate the server host key presented during the SSH handshake. known_hosts (请参阅指定已知主机)–(可选)将用于验证 SSH 握手期间提供的服务器主机密钥的密钥列表。 If this is not specified, the keys will be looked up in the file.ssh/known_hosts.如果未指定,则将在 file.ssh/known_hosts 中查找密钥。 If this is explicitly set to None, server host key validation will be disabled.如果明确设置为 None,服务器主机密钥验证将被禁用。

This is related but maybe not totally your salvation:这是相关的,但可能不完全是你的救恩:

https://github.com/ronf/asyncssh/issues/132 https://github.com/ronf/asyncssh/issues/132

The real question you should be asking yourself as you ask this question (help us help you) is where is it all failing?当你问这个问题(帮助我们帮助你)时,你应该问自己的真正问题是它在哪里失败了? Known-hosts via analogy is like env vars that don't show up when you need them to.类比的已知主机就像环境变量一样,在您需要它们时不会出现。

EDIT: Questions that immediately fire.编辑:立即引发的问题。 Host key is found but not trusted?找到主机密钥但不受信任? Hmm?唔?

EDIT2: Not trying to be harsh towards you but I think it's a helpful corrective. EDIT2:不想对你苛刻,但我认为这是一个有用的纠正。 You've got a software library that can find the key but is not known.您有一个可以找到密钥但不知道的软件库。 You're going to come across a lot of scenarios with SSH / shell / env var stuff where things you take for granted aren't known.你会遇到很多 SSH / shell / env var 的情况,你认为理所当然的事情是未知的。 Think clearly to help yourself and to ask the question better.清楚地思考以帮助自己并更好地提出问题。

With me, it runs smoothly after inserting known_hosts=None对我来说,插入known_hosts=None后运行流畅

Here's my example when trying the coding sample in Ortega book: I tried with hostname=ip/username/password of localCentOS, command test is ifconfig这是我在尝试 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

You should always validate the server's public key.您应该始终验证服务器的公钥。

Depending on your use case you can:根据您的用例,您可以:

  • Get the servers host keys, bundle them with your app and explicitly pass them to asyncssh (eg, as string with a path to your known_hosts file).获取服务器主机密钥,将它们与您的应用程序捆绑在一起,并将它们显式传递给 asyncssh(例如,作为带有您的 known_hosts 文件路径的字符串)。
  • Manually connect to the server on the command line.在命令行上手动连接到服务器。 SSH will then ask you if you want to trust the server. SSH 然后会询问您是否要信任该服务器。 The keys are then added to ~/.ssh/known_hosts and AsyncSSH will use them.然后将密钥添加到~/.ssh/known_hosts并且 AsyncSSH 将使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM