paramiko ssh通过主机名连接

时间:2019-06-13 01:47:01

标签: python

我有一个文本文件,其中包含ESXi主机名列表。我正在编写一个python程序以ssh进入每个程序,并获取命令输出。

当我提供IP地址时,它可以正常工作。但是当我提供主机名时,它将失败并显示错误:

Traceback (most recent call last):
  File "./esxi_version.py", line 10, in <module>
    ssh.connect(host,username='root',password='dangerous')
  File "/home/selva/.local/lib/python2.7/site-packages/paramiko/client.py", line 334, in connect
    to_try = list(self._families_and_addresses(hostname, port))
  File "/home/selva/.local/lib/python2.7/site-packages/paramiko/client.py", line 204, in _families_and_addresses
    hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
socket.gaierror: [Errno -2] Name or service not known

nslookup并挖掘到主机名工作正常。

请帮助。

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
all_host = open("all_esxi.txt","r")
output = all_host.readlines()
all_host.close()
for host in output:
    print (host)
    ssh.connect(host,username='xxxx',password='xxxxxx')
    stdin, stdout, stderr = ssh.exec_command("xxxxxx")
    version = stdout.readlines()
    print (version)`enter code here`
    ssh.close()

0 个答案:

没有答案