Handling tcpdump output in python

前端 未结 2 1099
死守一世寂寞
死守一世寂寞 2020-12-28 08:36

Im trying to handle tcpdump output in python.

What I need is to run tcpdump (which captures the packets and gives me information) and read the output and process it.

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 09:09

    You can make tcpdump line-buffered with "-l". Then you can use subprocess to capture the output as it comes out.

    import subprocess as sub
    
    p = sub.Popen(('sudo', 'tcpdump', '-l'), stdout=sub.PIPE)
    for row in iter(p.stdout.readline, b''):
        print row.rstrip()   # process here
    

提交回复
热议问题