#!/bin/bash
#此脚本是统计一定时间网卡的流量,平均值为秒为单位。
#要监控网卡地址
eth=eth0
#监控的秒数
sec=120
infirst=$(awk '/'$eth'/{print $1 }' /proc/net/dev |sed 's/'$eth'://')
outfirst=$(awk '/'$eth'/{print $10 }' /proc/net/dev)
sumfirst=$(($infirst+$outfirst))
sleep $sec"s"
inend=$(awk '/'$eth'/{print $1 }' /proc/net/dev |sed 's/'$eth'://')
outend=$(awk '/'$eth'/{print $10 }' /proc/net/dev)
sumend=$(($inend+$outend))
sum=$(($sumend-$sumfirst))
echo $sec"秒内总流量为:"$sum"bytes"
aver=$(($sum/$sec))
echo "平均流量为:"$aver" bytes/sec"
fudian=`echo "scale=3;"${aver}"/1024" |bc`
printf "平均流量为:""%2.3f MB/sec" $fudian
echo
exit