Search code examples
pythonmacossshparamiko

Paramiko "Unknown Server"


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:

import paramiko
ssh = paramiko.SSHClient()
ssh.connect('127.0.0.1', username='boatzart', password='mypassword')

The error I get is:

Traceback (most recent call last):
File "test.py", line 6, in <module>
ssh.connect('127.0.0.1')
File "build/bdist.macosx-10.7-intel/egg/paramiko/client.py", line 316, in connect
File "build/bdist.macosx-10.7-intel/egg/paramiko/client.py", line 85, in missing_host_key
paramiko.SSHException: Unknown server 127.0.0.1

This occurs no matter which server I try.


Solution

  • The exception was raised because you are missing a host key, the rather cryptic "Unknown server" is the clue - since the exception was raised from missing_host_key

    Try this instead:

    import paramiko
    
    paramiko.util.log_to_file('ssh.log') # sets up logging
    
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect('127.0.0.1', username=username, password=password)
    stdin, stdout, stderr = client.exec_command('ls -l')