I assume you only have two or three hosts in your host.txt
file. The culprits are the calls to Open_host.readline()
you make before the loop and at the end of each iteration, causing your script to skip the first host in the list, and one host out of two. Just removing those should solve your problem.
Here's the code, updated a bit to be more pythonic:
import subprocess
with open("hostlist.txt", "r") as hostlist, open("results.txt", "a") as output:
for host in hostlist:
host = host.strip()
print "Tracing", host
trace = subprocess.Popen(["tracert", "-w", "100", host], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
hop = trace.stdout.readline()
if not hop: break
print '-->', hop.strip()
output.write(hop)
# When you pipe stdout, the doc recommends that you use .communicate()
# instead of wait()
# see: http://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait
trace.communicate()