[英]Paramiko SSH connection error: socket.gaierror: [Errno 8] nodename nor servname provided, or not known
I have some problems with Python and Paramiko.我在使用 Python 和 Paramiko 时遇到了一些问题。 I want to connect to a SSH Server to automatically read some information from different systems.
我想连接到一个 SSH 服务器来自动读取来自不同系统的一些信息。
I tried some Tips I found here but nothing works.我尝试了一些在这里找到的提示,但没有任何效果。 I checked the hosts file, reinstall Python using brew, update/upgrade Python and Paramiko, used the ssh-server on my localhost to test.
我检查了主机文件,使用 brew 重新安装 Python,更新/升级 Python 和 Paramiko,在我的本地主机上使用 ssh-server 进行测试。 Also running pip to update paramiko and python.
还运行 pip 来更新 paramiko 和 python。
I am very confused.我很困惑。 Please let me ask you.
请让我问你。
This is my working environment:这是我的工作环境:
The Script: Basic Paramiko SSH Connection脚本:基本的 Paramiko SSH 连接
'''
Created on 16.09.2017
'''
import sys
import telnetlib
import paramiko
host = '213.000.000.123' #
user = "user"
password = "password"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, user, password)
Running the Script on Eclipse results with this output:在 Eclipse 上运行脚本结果如下:
Traceback (most recent call last):
File "/Users/tschaefer/Documents/workspace/PythonCMTSLibs/basicSSHSessionModule.py", line 17, in <module>
ssh.connect(host, user, password)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/client.py", line 301, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/client.py", line 199, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Ok, I find out gai=getaddrinfo, also getaddrbyname and so on.好的,我找到了 gai=getaddrinfo,还有 getaddrbyname 等等。 So I think there is a problem resolving the host-string to an IP Address.
所以我认为将主机字符串解析为 IP 地址存在问题。 But it doesn't make sense resolving an IP Address to an IP Address.
但是将 IP 地址解析为 IP 地址是没有意义的。 I used explicit IP.
我使用了显式 IP。 Using the "ssh 213.000.000.123" command directly on CLI works fine.
直接在 CLI 上使用“ssh 213.000.000.123”命令工作正常。
I also tried to connect to "localhost" or "127.0.0.1" or "google.com" or other.我还尝试连接到“localhost”或“127.0.0.1”或“google.com”或其他。 The hosts-file-entry is not commented so it's active.
hosts-file-entry 没有注释所以它是活动的。 Result: the same situation.
结果:同样的情况。
NSLOOKUP works correct, so the DNS Lookup works and a DNS server is available. NSLOOKUP 工作正常,因此 DNS 查找工作并且 DNS 服务器可用。
It seems Python could find the paramiko Library starting in Eclipse, but there is a problem in the Library. Python 似乎可以在 Eclipse 中找到 paramiko 库,但是库中存在问题。
Can anyone help me the see what my eye doesn't see.谁能帮我看看我的眼睛看不到的东西。
Thank a lot!非常感谢!
Greetings问候
The error message is very misleading.错误信息非常具有误导性。 It actually has nothing to do with DNS error.
它实际上与 DNS 错误无关。
Apparently the default port that paramiko uses is something other than 22. Once I specified a port, the problem goes away.显然 paramiko 使用的默认端口不是 22。一旦我指定了一个端口,问题就会消失。
port = 22
k = paramiko.RSAKey.from_private_key_file(key_filename)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
hostname = os.environ['DOCKER_EE_MANAGER_NODE']
username = os.environ['UCP_USER']
ssh.connect(hostname, port, username, pkey = k)
Works!有效! Enjoy!
享受!
In the Paramiko documentation you can see that the second positional argument to connect
is port
在Paramiko 文档中,您可以看到
connect
的第二个位置参数是port
connect(hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True, compress=False, sock=None, gss_auth=False, gss_kex=False, gss_deleg_creds=True, gss_host=None, banner_timeout=None, auth_timeout=None, gss_trust_dns=True, passphrase=None, disabled_algorithms=None)
连接(主机名,端口=22,用户名=无,密码=无,pkey=无,key_filename=无,超时=无,allow_agent=True,look_for_keys=True,compress=False,sock=None,gss_auth=False,gss_kex= False,gss_deleg_creds=True,gss_host=None,banner_timeout=None,auth_timeout=None,gss_trust_dns=True,passphrase=None,disabled_algorithms=None)
You are passing user
as the second argument, but connect
tries to interpret it as port
causing the connection to fail.您将
user
作为第二个参数传递,但connect
尝试将其解释为导致连接失败的port
。 Here is how you can fix the call to connect
using keyword arguments.以下是使用关键字参数修复
connect
调用的方法。
ssh.connect(host, username=user, password=password)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.