How to fix Paramiko SSHException: Server ... not found in known_hosts

Problem:

When trying to connect to a SSH server using paramiko using code like

ssh = paramiko.SSHClient()
ssh.connect("192.168.1.112")

you see an error message like

SSHException: Server '192.168.1.112' not found in known_hosts

Solution

The simplest solution is to add

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

before the call to connect():

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.112")

This will automatically add unknown host keys to the known hosts store. Note that this to some extent defeats the purpose of verifying host keys and you should manually verify host keys if possible. However, in many applications, there is no-one there to verify host keys in an automated process and thus this solution is more practical. Just keep in mind its security implications.