首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

LINUX上查看网卡流量的脚本(完美修改版)

2012-07-19 
LINUX下查看网卡流量的脚本(完美修改版)??? echo The network interface $eth is not exits!??? exit 1fi

LINUX下查看网卡流量的脚本(完美修改版)
??? echo The network interface $eth is not exits!
??? exit 1;
fi
outbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
inpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
outpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')
sleep $interval"s"
i=0
while [ "$i" -lt "$count" ]
do
?? inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
?? outbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
?? inpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
?? outpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')

?? bytesin=$(($inbytesend-$inbytesfirst))
?? bytesout=$(($outbytesend-$outbytesfirst))
?? packetsin=$(($inpacketsend-$inpacketsfirst))
?? packetsout=$(($outpacketsend-$outpacketsfirst))

?? if [ "$bytesin" -lt "0" ];then
????? bytesin=$((4294967295-$inbytesfirst+$inbytesend))
????? #echo bytesin $bytesin $inbytesfirst $inbytesend
?? fi
?? if [ "$bytesout" -lt "0" ];then
????? bytesout=$((4294967295-$outbytesfirst+$outbytesend))
????? #echo bytesout $bytesout $outbytesfirst $outbytesend
?? fi
?? if [ "$packetsin" -lt "0" ];then
????? packetsin=$((4294967295-$inpacketsfirst+$inpacketsend))
????? #echo packetsin $packetsin $inpacketsfirst $inpacketsend
?? fi
?? if [ "$packetsout" -lt "0" ];then
????? packetsout=$((4294967295-$outpacketsfirst+$outpacketsend))
????? #echo packetsout $packetsout $outpacketsfirst $outpacketsend
?? fi

?? bytesin=$(($bytesin/$interval))
?? bytesout=$(($bytesout/$interval))
?? packetsin=$(($packetsin/$interval))
?? packetsout=$(($packetsout/$interval))

?? sumbytesin=$(($sumbytesin+$bytesin))
?? sumbytesout=$(($sumbytesout+$bytesout))
?? sumpacketsin=$(($sumpacketsin+$packetsin))
?? sumpacketsout=$(($sumpacketsout+$packetsout))

?? if [ $(($i%20)) -eq 0 ];then
????? echo " ifname?? | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s"
????? echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
?? fi
?? echo $eth $bytesin $bytesout $packetsin $packetsout |awk '{printf("%9s | %10d %11d | %11d %12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'
?? inbytesfirst=$inbytesend
?? outbytesfirst=$outbytesend
?? inpacketsfirst=$inpacketsend
?? outpacketsfirst=$outpacketsend
??
?? i=$(($i+1))
?? sleep $interval"s"
done

sumbytesin=$(($sumbytesin/$i))
sumbytesout=$(($sumbytesout/$i))
sumpacketsin=$(($sumpacketsin/$i))
sumpacketsout=$(($sumpacketsout/$i))

echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
echo Average $sumbytesin $sumbytesout $sumpacketsin $sumpacketsout |awk '{printf("%9s | %10d %11d | %11d %12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'

输出示例:

[root@localhost nat]# ./ifstat.sh eth1 2 5
ifname?? | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s
--------- | ---------- ----------- | ----------- ------------ | ------------ -------------
???? eth1 |???? 204743????? 112824 |?????? 25592??????? 14103 |??????? 38459???????? 38172
???? eth1 |???? 212353????? 111547 |?????? 26544??????? 13943 |??????? 39025???????? 38152
???? eth1 |???? 204263????? 114007 |?????? 25532??????? 14250 |??????? 38802???????? 38233
???? eth1 |???? 208357????? 111877 |?????? 26044??????? 13984 |??????? 38706???????? 38123
???? eth1 |???? 215614????? 113400 |?????? 26951??????? 14175 |??????? 39950???????? 39375
--------- | ---------- ----------- | ----------- ------------ | ------------ -------------
Average |???? 209066????? 112731 |?????? 26133??????? 14091 |??????? 38988???????? 38411

热点排行