This code helps me make an ssh connection. I know that set_missing_host_key_policy
helps when the key is not found in the known_hosts
. But it is not behaving like the actual ssh
, because after the first time I run this code, I assumed that that the host_key
would be added to known_hosts
and that I need not have the function set_missing_host_key_policy()
anymore. But, I was wrong (paramiko.ssh_exception.SSHException)
. How can I permanently add the host_key
to known_hosts
using paramiko
? (As a certain part of the backend code is written in 'C' and it needs the host_key
to be found in known_hosts
)
Or am I misunderstanding something? I would need some guidance on this...
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))
If you want to add one specific key in runtime (without any file):
source: https://github.com/paramiko/paramiko/blob/2.6.0/tests/test_hostkeys.py#L75-L84
From the package documentation, compare
with
So to make Paramiko store any new host keys, you need to use
load_host_keys
, notload_system_host_keys
. E.g.But it's generally a good idea to avoid using
AutoAddPolicy
, since it makes you open to man-in-the-middle attacks. What I ended up doing was to generate a localknown_hosts
in the same folder as the script:and then load this file instead:
This way I can distribute the
known_hosts
together with my script and run it on different machines without touching the actual known_hosts on those machines.