relay icon indicating copy to clipboard operation
relay copied to clipboard

paramiko does not automatically add unknown hosts

Open onyxfish opened this issue 11 years ago • 11 comments

Connecting to SSH session on remote port 2222
Traceback (most recent call last):
  File "/usr/local/share/python/relay", line 9, in <module>
    load_entry_point('relay==0.0.8', 'console_scripts', 'relay')()
  File "/usr/local/lib/python2.7/site-packages/relay.py", line 187, in _main
    func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/relay.py", line 281, in ssh
    key_filename=env['pair_private_key']
  File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 321, in connect
    self._policy.missing_host_key(self, server_hostkey_name, server_key)
  File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 85, in missing_host_key
    raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.SSHException: Server '[54.244.243.254]:2222' not found in known_hosts

onyxfish avatar Mar 14 '13 14:03 onyxfish

Hi,

I have the same problem. Is this a bug or I'm doing something wrong. I'm using paramiko in Python 2.7 32 bits over Windows 7.

jmsalomr avatar Oct 17 '13 10:10 jmsalomr

Sorry, solved.

You have to use:

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) before

jmsalomr avatar Oct 17 '13 10:10 jmsalomr

You need add: ssh.load_system_host_keys() ..before ssh.connect

ghost avatar May 31 '14 05:05 ghost

+1 @jmsalomr , that worked. Thanks.

anirudt avatar Dec 25 '15 06:12 anirudt

@jmsalomr you saved my time. Thanks!!

anunayamar avatar Feb 10 '16 21:02 anunayamar

+1 @jmsalomr , that worked. Thanks.

zlshi avatar Aug 23 '16 09:08 zlshi

@jmsalomr Thanks.

tao12345666333 avatar Oct 31 '16 14:10 tao12345666333

@jmsalomr thanks :) :+1:

AlexandreLicinio avatar Sep 11 '17 10:09 AlexandreLicinio

To modify ~/.ssh/known_hosts file (to save the keys permanently) in addition to AutoAddPolicy(), use client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')). Note: AutoAddPolicy() opens you to the MITM attack. See Paramiko: Add host_key to known_hosts permanently.

zed avatar Feb 02 '18 10:02 zed

import paramiko as paramiko
from paramiko import AutoAddPolicy

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect('52.160.xx.xx', port=22, username='frt-user', password='xxxxxx',sock=None)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls')

i get this error TypeError: 'NoneType' object is not callable

what is the step i am missing or doing wrong? @jmsalomr

usama767 avatar Aug 13 '20 13:08 usama767

@usama767

I ran into both of the issues in this thread. First, adding the autopolicy does work most of the time.

To fix the NoneType error, I went to //.ssh and deleted everything. Started working.

akliorin avatar Dec 04 '20 23:12 akliorin