Q: paramiko/SSH/ how to get a remote host_key

Guilherme Polo ggpolo at gmail.com
Wed Jan 30 20:04:38 CET 2008


2008/1/30, Charles_hans <charles_hans at yahoo.com>:
>
> I tried to get what host_key has been aquired after AutoPolicy is set. I
> added the following code just before client.close() in rosty's final code:
>
> try:
>     host_keys =
> paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
> except IOError:
>     try:
>         host_keys =
> paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts'))
>     except IOError:
>         print '*** Unable to open host keys file'
>
> I still got 'Unable to open host keys file'. Can you tell me how to get the
> remote host_key under this situation? Thanks!
>
> Charles
> 1/30/2008

Hey Charles,

If you take a look on your code, you will see that you are catching
IOError. So the problem you are noticing is related to I/O failing
such as non-existent file. Be sure to check if '~/.ssh/known_hosts'
exists, if the first try fails, check if "~/ssh/known_hosts" exists
then (since you are trying to access that file).

Cheers,

>
> by Guilherme Polo Jan 21, 2008; 09:08am :
>
> 2008/1/21, DHR <dima.hristov at gmail.com>:
>
> Very nice =)
>
> Just an advice, you dont need to import base64. Method decode of
> strings allows you to specify encoding as 'base64' to perform needed
> operations.
>
>
> by rosty Jan 21, 2008; 08:43am :
>
> Thank you! Now it works and the code looks like this:
>
> import paramiko
> import base64
> from paramiko import AutoAddPolicy, SSHClient
>
> client = paramiko.SSHClient()
> client.set_missing_host_key_policy(AutoAddPolicy())
> client.connect('hostIP', username='uname', password='pass')
> stdin, stdout, stderr = client.exec_command('ls')
> for line in stdout:
>     print '... ' + line.strip('\n')
>
> client.close()
> --
> View this message in context: http://www.nabble.com/Q%3A-paramiko-SSH--how-to-get-a-remote-host_key-tp14996119p15189222.html
> Sent from the Python - python-list mailing list archive at Nabble.com.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
-- Guilherme H. Polo Goncalves



More information about the Python-list mailing list