查看网卡流量的脚本

几点说明 :

  1. 该脚本用来放在服务器上查看网卡的目前流量.
  2. 该脚本只是大概统计流量,由于执行命令行需要消耗时间,故结果并不准确.但误差不大,可以看出流量趋势.
  3. 脚本的使用方法sh stream.sh $seconds,比如sh stream.sh 10表示10秒统计一次,sh stream.sh 1表示1秒一次.

#!/bin/bash
#monitor streams of LTS channels
#Write by calfen 20090227

timeOld=`date +%s`
eth0=`cat /proc/net/dev | grep eth0 | sed 's=^.*:==' `
eth1=`cat /proc/net/dev | grep eth1 | sed 's=^.*:==' `
eth0InOld=$(echo $eth0 | awk '{ print $1 }')
eth0OutOld=$(echo $eth0 | awk '{ print $9 }')
eth1InOld=$(echo $eth1 | awk '{ print $1 }')
eth1OutOld=$(echo $eth1 | awk '{ print $9 }')
while true
do
sleep ${1}


eth0=`cat /proc/net/dev | grep eth0 | sed 's=^.*:==' `
eth1=`cat /proc/net/dev | grep eth1 | sed 's=^.*:==' `
timeNew=`date +%s`
eth0InNew=$(echo $eth0 | awk '{ print $1 }')
eth0OutNew=$(echo $eth0 | awk '{ print $9 }')
eth1InNew=$(echo $eth1 | awk '{ print $1 }')
eth1OutNew=$(echo $eth1 | awk '{ print $9 }')
eth0DiffIn=$((eth0InNew-eth0InOld))
eth1DiffIn=$((eth1InNew-eth1InOld))
eth0DiffOut=$((eth0OutNew-eth0OutOld))
eth1DiffOut=$((eth1OutNew-eth1OutOld))
timeDiff=$((timeNew-timeOld))
echo "eht0-In: $((eth0DiffIn*8/1024/timeDiff)) kbps eth0-Out: $((eth0DiffOut*8/1024/timeDiff)) kbps eht1-IN: $((eth1D
iffIn*8/1024/timeDiff)) kbps eth1-Out: $((eth1DiffOut*8/1024/timeDiff)) kbps"
eth0InOld=${eth0InNew}
eth1InOld=${eth1Inew}
eth0OutOld=${eth0OutNew}
eth1OutOld=${eth1OutNew}
timeOld=${timeNew}
done

评论

此博客中的热门博文

每逢中秋倍思亲