python 怎么调用linux下的tcpdump
1个回答
2016-09-07 · 知道合伙人软件行家
关注
展开全部
之前在linux用python脚本写一个抓包分析小工具,实在不想用什么libpcap、pypcap所以,简单来了个tcpdump加grep搞定。基本思路是分别起tcpdump和grep两个进程,进程直接通过pipe交换数据,简单代码如下:
[python] view plain copy
#! /usr/bin/python
def tcpdump():
import subprocess, fcntl, os
# sudo tcpdump -i eth0 -n -s 0 -w - | grep -a -o -E "Host: .*|GET /.*"
cmd1 = ['tcpdump', '-i', 'eth0', '-n','-B', '4096','-s', '0', '-w', '-']
cmd2 = ['grep', '--line-buffered', '-a', '-o', '-E', 'Host: .*|GET /.*']
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()
while True:
text = poll_tcpdump(proc)
if text:
print '>>>> ' + text
[python] view plain copy
#! /usr/bin/python
def tcpdump():
import subprocess, fcntl, os
# sudo tcpdump -i eth0 -n -s 0 -w - | grep -a -o -E "Host: .*|GET /.*"
cmd1 = ['tcpdump', '-i', 'eth0', '-n','-B', '4096','-s', '0', '-w', '-']
cmd2 = ['grep', '--line-buffered', '-a', '-o', '-E', 'Host: .*|GET /.*']
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()
while True:
text = poll_tcpdump(proc)
if text:
print '>>>> ' + text
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询