#!/usr/bin/env python import time def tcpdump(): import subprocess, fcntl, os # sudo tcpdump -i eth0 -n -s 0 -w - | grep -a -o -E "Host: .*|GET /.*" cmd1 = ['tcpdump','tcp', '-i', 'eth0','port','80','-n','-B', '4096','-s', '0', '-w', '-'] #cmd2 = ['grep', '--line-buffered', '-a', '-o', '-E', 'Host: .*|GET /.*|POST /.*|Last-Modified: .*|Date: .*'] cmd2 = ['grep', '--line-buffered', '-a', '-o', '-E', 'Host: .*|GET /.*|POST /.*'] p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE) p2 = subprocess.Popen(cmd2, stdout=subprocess.PIPE, stdin=p1.stdout) flags = fcntl.fcntl(p2.stdout.fileno(), fcntl.F_GETFL) fcntl.fcntl(p2.stdout.fileno(), fcntl.F_SETFL, (flags | os.O_NDELAY | os.O_NONBLOCK)) return p2 def poll_tcpdump(proc): #print 'poll_tcpdump....' import select txt = None while True: # wait 1/10 second readReady, _, _ = select.select([proc.stdout.fileno()], [], [], 0.1) if not len(readReady): break try: for line in iter(proc.stdout.readline, ""): if txt is None: txt = '' txt += line except IOError: #print('data empty...') pass break return txt proc = tcpdump() start=int(time.time()) stop=int(time.time()) history=[] while (stop-start)<60: text = poll_tcpdump(proc) if text: #print('>>>> ' + text) history.append(text) stop=int(time.time()) with open('./tcpdump.txt','w', encoding='utf-8') as f: f.write('\n'.join(history)) # while True: # text = poll_tcpdump(proc) # if text: # print('>>>> ' + text) # cat tcpdump.txt | grep -Ev '.jpg|.png|.js|.css|ttf|woff|gif|Host' | more