1
我知道這個腳本在這裏曾經討論過,但我仍然無法正確運行。問題是逐行讀取文本文件。在舊腳本traceroute multiple hos python。 (逐行讀取.txt)
while host:
print host
使用,但使用這種方法程序崩潰,所以我決定將它更改爲
for host in Open_host:
host = host.strip()
,但使用這個腳本只給出.txt文件的最後一行的結果。有人可以幫助我使它工作嗎? 下sript:
# import subprocess
import subprocess
# Prepare host and results file
Open_host = open('c:/OSN/host.txt','r')
Write_results = open('c:/OSN/TracerouteResults.txt','a')
host = Open_host.readline()
# loop: excuse trace route for each host
for host in Open_host:
host = host.strip()
# execute Traceroute process and pipe the result to a string
Traceroute = subprocess.Popen(["tracert", '-w', '100', host],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
hop = Traceroute.stdout.readline()
if not hop: break
print '-->',hop
Write_results.write(hop)
Traceroute.wait()
# Reading a new host
host = Open_host.readline()
# close files
Open_host.close()
Write_results.close()
感謝名單,現在它正在工作 – Tomas