I\'m trying to get started with the Paramiko library, but the library is throwing an exception as soon as I try to connect with the following simple program:
The correct way is either:
Call the HostKeys.add on the instance returned by SSHClient.get_host_keys before calling the connect
, passing it the trusted key.
from base64 import decodebytes
# ...
keydata = b"""AAAAB3NzaC1yc2EAAAADAQAB..."""
key = paramiko.RSAKey(data=decodebytes(keydata))
client.get_host_keys().add('example.com', 'ssh-rsa', key)
To see how to obtain the fingerprint for use in the code, see my answer to:
Verify host key with pysftp.
If you know a fingerprint only, see:
Python - pysftp / paramiko - Verify host key using its fingerprint
Or load the already cached hostkey (e.g. by the command-line ssh
) using client.load_system_host_keys().
Or you can at least cache the hostkey from the first attempt to make sure it does not change in the future.
For that use SSHClient.load_host_keys before connect
. It makes Paramiko automatically add the new host key to the file (when combined with AutoAddPolicy).