Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 470
rcv Avatar asked May 20 '12 01:05

rcv


People also ask

How do I connect to Paramiko?

A Paramiko SSH Example: Connect to Your Server Using a Password. This section shows you how to authenticate to a remote server with a username and password. To begin, create a new file named first_experiment.py and add the contents of the example file. Ensure that you update the file with your own Linode's details.

Does Paramiko use OpenSSH?

Paramiko does not itself leverage OpenSSH-style config file directives, but it does implement a parser for the format, which users can honor themselves (and is used by higher-level libraries, such as Fabric).

What is Set_missing_host_key_policy?

set_missing_host_key_policy (policy) Set policy to use when connecting to servers without a known host key.

What is Paramiko transport?

class paramiko.Transport. An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called Channels, across the session. class paramiko.SSHClient. A high-level representation of a session with an SSH server.


2 Answers

I experienced the same issue and here's the solution that worked out for me:

import paramiko  client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('127.0.0.1', username=username, password=password) stdin, stdout, stderr = client.exec_command('ls -l') 

This is to set the policy to use when connecting to a server that doesn't have a host key in either the system or local HostKeys objects. The default policy is to reject all unknown servers (using RejectPolicy). You may substitute AutoAddPolicy or write your own policy class.

More details at paramiko api doc. Hope this helps.

After that you can save into an other keyfile file for next usage as follows.

ssh.get_host_keys().save('/some/file/path') 

You can always load from a file as follows.

ssh.load_host_keys('/some/file/path') 
like image 190
user1224821 Avatar answered Sep 27 '22 22:09

user1224821


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') 
like image 43
Burhan Khalid Avatar answered Sep 27 '22 22:09

Burhan Khalid