2015-03-13 43 views
0

我是新来的python和工作traceroute,所以我想知道是否有可能将整个python traceroute结果输出到txt和Csv文件?任何想法我怎么能做到这一点,因为我找不到任何适当的显示如何做到这一点。是否可以将整个python输出写入文本或csv文件?

感谢

+0

它需要从python脚本内完成编写O/P! – 2015-03-13 18:51:22

回答

2
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() 

我从主机列表读取主机的详细信息,并在文件中

相关问题