#!/bin/bash
. /etc/profile #解决which tcpdump ifconfig找不到的问题
savapath=XXXXX #保存路径
dayout=XXX #保存多少天的值
maxfilesize= XXX #文件的大小
netnames=$* #传入网口的名称,如果不传入,则默认监视所以网卡,传入模式eth0 eth1
G_captool=`which tcpdump`
G_ifconfig=`which ifconfig`
if [ ! -d $savapath ]; then
mkdir $savapath
fi
if [ -z "$netnames ]; then
#列出所有网卡,屏蔽lo
netlist= `$G_ifconfig | grep -o ^[a-z0-9]* | grep -v lo`
else
netlist=$netnames
fi
dt_str=`date + "F"`
#遍历网卡
for net in $netlist
do
#查找非今天的抓包进程,全部杀死
procid=`ps aux | grep $G_captool | grep $ savapath | grep -v $dt_str | grep $net | grep -v grep | awk '{print $2}' | xargs`
if [ ! -z "$procid"]: then
kill -9 $procid >/dev/null
fi
#查找今天的抓包进程,能找到则不在抓包,50M一个文件
procid=`ps aux | grep $G_captool | grep $ savapath | grep $dt_str | grep $net | grep -v grep | awk '{print $2}' | xargs`
if [ ! -z "$procid"]: then
$G_captool tcp -i $net -C $maxfilesize -w $savapath/${net}_`date + "F_%H%M%S"`.cap &
fi
done
#删除多少天前的文件
find $savapath -type f -mtime +$dayout -exec rm -f {} \;