#!/bin/bash TCycle=60 path_count="/var/traf_count" FileTMP="/tmp/traf" IPtables="/sbin/iptables" function in_calc () { echo $1 $IPtables -L INPUT -v -x -Z > $FileTMP for x in $1; do counters=`cat $FileTMP | grep $x | grep ACCEPT | awk '{print $2}'` if [ ! -e "$path_count/$x-in.cnt" ]; then touch "$path_count/$x-in.cnt"; fi if [ `echo $counters | wc -w` != `cat "$path_count/$x-in.cnt" | wc -w` ]; then echo "$counters" > "$path_count/$x-in.cnt" else n=1; new_str=""; for y in $counters; do val=`cat "$path_count/$x-in.cnt" | sed -n ${n}p` let val=val+y; let n=n+1; new_str=${new_str}${val}$'\n' done echo "$new_str" > "$path_count/$x-in.cnt" fi done rm $FileTMP } function out_calc () { echo $1 $IPtables -L OUTPUT -v -x -Z > $FileTMP for x in $1; do counters=`cat $FileTMP | grep $x | grep ACCEPT | awk '{print $2}'` if [ ! -e "$path_count/$x-out.cnt" ]; then touch "$path_count/$x-out.cnt"; fi if [ `echo $counters | wc -w` != `cat "$path_count/$x-out.cnt" | wc -w` ]; then echo "$counters" > "$path_count/$x-out.cnt" else n=1; new_str=""; for y in $counters; do val=`cat "$path_count/$x-out.cnt" | sed -n ${n}p` let val=val+y; let n=n+1; new_str=${new_str}${val}$'\n' done echo "$new_str" > "$path_count/$x-out.cnt" fi done rm $FileTMP } while [ "1" = "1" ]; do in_calc "eth1 eth0 lo ppp+"; out_calc "eth1 eth0 lo ppp+"; sleep $TCycle done