我有一個python腳本,想ping幾個(相當於幾個!)主機。我已經將它設置爲讀取hosts.txt文件的內容作爲主機在腳本中進行ping操作。奇怪的是,我recieving下面的錯誤,在最初的幾個地址(無論它們是什麼):Python腳本中的ping請求失敗
Ping request could not find host 66.211.181.182. Please check the name and try again.
我已經包含如上圖所示,在兩次(在文件中)的地址,它會嘗試平。任何想法,我做錯了 - 我是一個蟒蛇新手,所以要溫柔。
這裏是我的腳本:
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)
@EliAcherkan,這是有道理的,我想.. 。當我測試這個閱讀部分時,我只是簡單地打印了這些線條,它打破了線條。我將如何克服這一點? – MHibbin
感謝那 – MHibbin