开发者

"getaddrinfo" error when trying to establish an SSH connection using Python Paramiko

开发者 https://www.devze.com 2023-02-16 22:32 出处:网络
Using Paramiko I am trying to establish a connection with a server, but that connection is failing with the following output

Using Paramiko I am trying to establish a connection with a server, but that connection is failing with the following output

Traceback (most recent call last):
  File "C:\ucatsScripts\cleanUcatsV2.py", line 13, in <module>
    ssh.connect(host,username,password)
  File "C:\Python27\lib\site-packages\paramiko-1.7.6-py2.7.egg\paramiko\client.py", line 278, in connect
    for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM开发者_StackOverflow社区):
socket.gaierror: [Errno 10109] getaddrinfo failed

Here is the code I am using

import paramiko
import cmd
import sys

# Connect to Server
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())

success = ssh.connect('MASKED',username='MASKED',password='MASKED')
if (success != True):
    print "Connection Error"
    sys.exit()
else:
    print "Connection Established"

any ideas?


Just add the port after the host and you'll be set:

ssh.connect('MASKED', 22, username='MASKED',password='MASKED')

BTW, as robots.jpg said, the connect method doesn't return anything. Instead of returning values it triggers exceptions.

Here is a more complete example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import paramiko, os, string, pprint, socket, traceback, sys

time_out = 20 # Number of seconds for timeout
port     = 22
pp = pprint.PrettyPrinter(indent=2)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

file = open( "file.txt", "r" )
# NOTE: The file contains:    
# host  user  current_password

array = []

for line in file:
  array = string.split(line.rstrip('\n'),)
#  pp.pprint(array)
  try:
    ssh.connect(array[0], port, array[1], array[2], timeout=time_out)
    print "Success!! -- Server: ", array[0], "   Us: ", array[1]
  except paramiko.AuthenticationException:
    print "Authentication problem   -- Server: ", array[0], "   User: ", array[1]
    continue
  except socket.error, e:
    print "Comunication problem    -- Server: ", array[0], "   User: ", array[1]
    continue
  ssh.close()

file.close()

The code needs some polish but it does the job.


Be care to didn't have the username on your hostname

ssh.connect(hostname='user@example.com', port=22)

user@example.com isn't a hostname parameter that fit for the connection.

You should use:

ssh.connect(hostname='example.com', port=22, username='user')


Are you sure the hostname resolves to IP address? Try ping that_hostname on your machine to see.


You need to make some changes for DNS. Go to Network Connections -> IPv4-> Advanced Settings -> DNS then check Append these DNS suffixes, and enter your machine dns. This worked for me. Anyway this error comes from the Ethernet settings. Good Luck !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号