我有一个 python 脚本想要 ping 一些(相当多!)主机。我已将其设置为读取 hosts.txt 文件的内容作为要在脚本中 ping 的主机。奇怪的是,对于前几个地址(无论它们是什么),我收到以下错误:
Ping request could not find host 66.211.181.182. Please check the name and try again.
我已经两次(在文件中)包含了上面显示的地址,它会尝试 ping。关于我做错了什么的任何想法 - 我是一个 python 新手,所以要温柔。
这是我的脚本:
import subprocess
hosts_file = open("hosts.txt","r")
lines = hosts_file.readlines()
for line in lines:
ping = subprocess.Popen(
["ping", "-n", "1",line],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
out, error = ping.communicate()
print out
print error
hosts_file.close()
这是我的 hosts.txt 文件:
66.211.181.182
178.236.5.39
173.194.67.94
66.211.181.182
以下是上述测试的结果:
Ping request could not find host 66.211.181.182
. Please check the name and try again.
Ping request could not find host 178.236.5.39
. Please check the name and try again.
Ping request could not find host 173.194.67.94
. Please check the name and try again.
Pinging 66.211.181.182 with 32 bytes of data:
Request timed out.
Ping statistics for 66.211.181.182:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss)