Paramiko SSH failing with "Server '...' not found in known_hosts" when run on web server
You can hard-code the host key in your Python code, using HostKeys.add
:
import paramikofrom base64 import decodebyteskeydata = b"""AAAAB3NzaC1yc2EAAAABIwAAAQEA0hV..."""key = paramiko.RSAKey(data=decodebytes(keydata)) client = paramiko.SSHClient()client.get_host_keys().add('example.com', 'ssh-rsa', key)client.connect(...)
This is based on my answer to:
Paramiko "Unknown Server".To see how to obtain the fingerprint for use in the code, see my answer to:
Verify host key with pysftp.If using pysftp, instead of Paramiko directly, see:
PySFTP failing with "No hostkey for host X found" when deploying Django/Heroku
Or, as you are connecting within a private network, you can give up on verifying host key altogether, using AutoAddPolicy
:
client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())client.connect(...)
(This can be done only if you do not need the connection to be secure, like when connecting within a private network)