Unable to connect to remote host using paramiko?

24,813

Solution 1

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

Solution 2

For me the solution was:

client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(host, username=user,password=password)

Solution 3

Try using this:

ssh.connect('host', username='username',password='password')

You can also add your public key to known hosts in server, if you wish to skip password and connect without giving your password. In that case use:

ssh.connect('host', username='username')
Share:
24,813
SaiKiran
Author by

SaiKiran

Programmer

Updated on July 09, 2022

Comments

  • SaiKiran
    SaiKiran almost 2 years

    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.