19

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('somename@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 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.

SaiKiran
  • 6,244
  • 11
  • 43
  • 76

3 Answers3

40

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

tokhi
  • 21,044
  • 23
  • 95
  • 105
6

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)
Eric
  • 71
  • 1
  • 1
  • 4
    Care to explain that solution a bit better? Just pasting some code is ok, but please elaborate on the reasons why this code fixes or solves the problem. – IncredibleHat Nov 03 '17 at 19:58
1

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')
realr
  • 3,652
  • 6
  • 23
  • 34
troy_achilies
  • 592
  • 1
  • 7
  • 15