“获取地址信息”尝试使用 Python Paramiko 建立 SSH 连接时出错

发布于 2024-10-21 18:23:58 字数 925 浏览 5 评论 0原文

我正在尝试使用 Paramiko 与服务器建立连接,但该连接失败并显示以下输出

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"

,有什么想法吗?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还给你自由 2024-10-28 18:23:58

只需在主机后面添加端口即可:

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

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

这是一个更完整的示例:

#!/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()

代码需要一些改进,但它可以完成工作。

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.

ゞ花落谁相伴 2024-10-28 18:23:58

请注意您的主机名上没有用户名

ssh.connect(hostname='[email protected]', port=22)

[email protected]< /a> 不是适合连接的主机名参数。

你应该使用:

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

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

ssh.connect(hostname='[email protected]', port=22)

[email protected] isn't a hostname parameter that fit for the connection.

You should use:

ssh.connect(hostname='example.com', port=22, username='user')
傻比既视感 2024-10-28 18:23:58

您确定主机名解析为 IP 地址吗?尝试在您的计算机上 ping that_hostname 来查看。

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

无悔心 2024-10-28 18:23:58

您需要对 DNS 进行一些更改。
转到网络连接 -> IPv4->高级设置-> DNS 然后选中附加这些 DNS 后缀,并输入您的计算机 dns。
这对我有用。无论如何,这个错误来自以太网设置。
祝你好运 !

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文