“获取地址信息"尝试使用 Python Paramiko 建立 SSH 连接时出错 [英] "getaddrinfo" error when trying to establish an SSH connection using Python Paramiko

查看:58
本文介绍了“获取地址信息"尝试使用 Python Paramiko 建立 SSH 连接时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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:\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):
socket.gaierror: [Errno 10109] getaddrinfo failed

这是我使用的代码

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"

有什么想法吗?

推荐答案

在host后面加上端口就可以了:

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

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

顺便说一句,正如 robots.jpg 所说,connect 方法不返回任何内容.它不会返回值,而是触发异常.

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.

这篇关于“获取地址信息"尝试使用 Python Paramiko 建立 SSH 连接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆