标签:
网络收发包计数记录在 /proc/net/dev 文件中, 要取得流量, 只需要读取里面的内容两次, 然后相减, 再除以时间间隔即可.
#!/bin/bash
#Usage1,record in file: netmonitor <MinKB> <Interval> <FileName>
#Usage2,print on terminal: netmonitor <MinKB> <Interval>
#Record the value great than 100KB, 3 second Interval:
#Example1,record: netmonitor 100 3 xxx.txt
#Example2,print: netmonitor 100 3
#参数说明
#第一个是速度记录的下限,单位是KB,即超过多少KB才记录,0为全部记录
#第二个参数是采样间隔,即隔多少秒记录一次
#第三个参数是输出的文件名,为空的话就直接在终端打印
#xieqianli
#2011-4-25
#同时修改下列变量可以固定参数,使用脚本时就无需参数
MinKB="$1"
Interval="$2"
FileName="$3"
NetDev="wlan0"
#检查参数完整性
if [ "$Interval" == "" ];then
echo "Usage1,record in file: netmonitor <MinKB> <Interval> <FileName>"
echo "Usage2,print on terminal: netmonitor <MinKB> <Interval>"
echo "Record the values great than 100KB, 3 seconds Interval:"
echo "Example1,record: netmonitor 100 3 xxx.txt"
echo "Example2,print: netmonitor 100 3"
exit 1
fi
#检查 MinKB 参数的合法性
check=`echo $MinKB | grep "[^0-9]"`
if [ "$check" != "" ];then
echo "$MinKB is not a number"
exit 0
fi
len=${#MinKB}
char1=`echo $MinKB | cut -c 1`
if [ $(($len)) -gt $((1)) -a "$char1"=="0" ];then
echo "$MinKB is not a number"
exit 0
fi
#检查 Interval 参数的合法性
check=`echo $Interval | grep "[^0-9]"`
if [ "$check" != "" ];then
echo "$Interval is not a number"
exit 0
fi
len=${#Interval}
char1=`echo $Interval | cut -c 1`
if [ $(($len)) -gt $((1)) -a "$char1"=="0" ];then
echo "$Interval is not a number"
exit 0
fi
while [ 1 ]
do
#读取已接收和已发送字节数,间隔一秒
REC1=`cat /proc/net/dev | grep $NetDev | sed ‘s/:/ /‘ | tr -s " " | cut -d "