Unable to connect to remote host using paramiko?

SaiKiran picture SaiKiran · Jan 18, 2017 · Viewed 18.5k times · Source

I want to transfer files between two Ubuntu Servers using scp, i have tested scp between the two systems and it worked perfectly fine.So i dont want to execute the command everytime i need to get files so i want to write a python script which automatically downloads files from other host using scp.

While searching online i found this Paramiko module and i have trouble installing this and i have rectified this using module cryptography.Now the real trouble is explained with the terminal below.

>>> from paramiko import SSHClient
>>> from scp import SCPClient
>>> ssh = SSHClient()
>>> ssh
<paramiko.client.SSHClient object at 0x1a41c90>
>>> ssh.load_system_host_keys()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
>>> ssh.connect('[email protected]')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 296, in c                                                                                    onnect
    to_try = list(self._families_and_addresses(hostname, port))
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 200, in _                                                                                    families_and_addresses
    addrinfos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_S                                                                                    TREAM)
socket.gaierror: [Errno -2] Name or service not known
>>> ssh.connect('192.168.100.100')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 361, in c                                                                                    onnect
    server_key)
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 672, in m                                                                                    issing_host_key
    raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.100.100' not found in known_hos                                                                                    ts

I have changed the ip and username for safe use somename is replaced but i have tried with original username.So i tried this several times but i still getting the same error.

Any suggestions on this problem?Please Help.

Answer

tokhi picture tokhi · Jan 18, 2017

Maybe you are missing the missing_host_key_policy

What about this one:

proxy = None
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host['hostname'], username=host['user'], sock=proxy)

more examples here: www.programcreek.com