如何在 paramiko 连接中临时添加 host_key

How to add host_key temporarily in a paramiko connection

我正在使用 Paramiko 连接到 SSH 服务器,我想临时添加 "host_keys"。

我该怎么做?

import paramiko

client = paramiko.SSHClient()
client.load_system_host_keys()
#client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=str(host),username =str(user),password=str(pswd))
from paramiko import RSAKey
from paramiko.py3compat import decodebytes

client = SSHClient()

# known host key
know_host_key = "<KEY>"
keyObj = RSAKey(data=decodebytes(know_host_key.encode()))

# add to host keys
client.get_host_keys().add(hostname=HOST, keytype="ssh-rsa", key=keyObj)

# login to ssh hostname
client.connect(hostname=HOST, port=PORT, username=USER)...

来源:https://github.com/paramiko/paramiko/blob/2.6.0/tests/test_hostkeys.py#L75-L84