Created
September 8, 2011 14:20
-
-
Save m-mizutani/1203508 to your computer and use it in GitHub Desktop.
tcpdump rotator in shell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
PID_FILE=$HOME/local/var/run/`basename $0`.pid | |
NIC=en0 | |
DATA_DIR=$HOME/local/var/data | |
FNAME_PREFIX="dump" | |
FILTER="port not 80" | |
if [ "`id -u`" != "0" ] | |
then | |
echo "need root privillege" | |
exit | |
fi | |
if [ $# -ne 1 ] | |
then | |
echo "syntax)" `basename $0` "[start|rotate|stop]" | |
exit | |
fi | |
mkdir -p `dirname $PID_FILE` | |
mkdir -p $DATA_DIR | |
stop () { | |
if [ -e $PID_FILE ] | |
then | |
kill `cat $PID_FILE` | |
rm $PID_FILE | |
fi | |
} | |
start () | |
{ | |
SUFFIX=`date "+%Y%m%d%H%M%S"` | |
tcpdump -s 0 -w $DATA_DIR/$FNAME_PREFIX-$SUFFIX.pcap -ni $NIC $FILTER 2> /dev/null & | |
disown | |
PID=$! | |
echo $PID > $PID_FILE | |
} | |
case $1 in | |
"start") | |
start | |
;; | |
"rotate") | |
stop | |
start | |
;; | |
"stop") | |
stop | |
;; | |
*) | |
echo "syntax)" `basename $0` "[start|rotate|stop]" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment