Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

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

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:ucatsScriptscleanUcatsV2.py", line 13, in <module>
    ssh.connect(host,username,password)
  File "C:Python27libsite-packagesparamiko-1.7.6-py2.7.eggparamikoclient.py", line 278, in connect
    for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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('
'),)
#  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...