Reputation: 1
i'm new to python and working on traceroute so I was wondering if it's possible to write entire python traceroute result output to txt and Csv file? any idea how can i achieve that because i can't find any proper one which shows how to do it.
thanks
Upvotes: 0
Views: 257
Reputation: 8154
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()
I am reading host details from hostlist and writing o/p in a file
Upvotes: 2