Shell腳本實(shí)現(xiàn)的單機(jī)流量統(tǒng)計(jì)功能
在網(wǎng)上看到這個(gè)單機(jī)流量的腳本,挺不錯(cuò)的。
#!/bin/sh
usage(){
echo “Usage: $0 [-i INTERFACE] [-s INTERVAL] [-c COUNT]”
echo
echo “-i INTERFACE”
echo “ The interface to monitor, default is eth0.”
echo “-s INTERVAL”
echo “ The time to wait in seconds between measurements, default is 3 seconds.”
echo “-c COUNT”
echo “ The number of times to measure, default is 10 times.”
exit 3
}
readargs(){
while [ "$#" -gt 0 ] ; do
case “$1″ in
-i)
if [ "$2" ] ; then
interface=”$2″
shift ; shift
else
echo “Missing a value for $1.”
echo
shift
usage
fi
;;
-s)
if [ "$2" ] ; then
sleep=”$2″
shift ; shift
else
echo “Missing a value for $1.”
echo
shift
usage
fi
;;
-c)
if [ "$2" ] ; then
counter=”$2″
shift ; shift
else
echo “Missing a value for $1.”
echo
shift
usage
fi
;;
*)
echo “Unknown option $1.”
echo
shift
usage
;;
esac
done
}
checkargs(){
if [ ! "$interface" ] ; then
interface=”eth0″
fi
if [ ! "$sleep" ] ; then
sleep=”3″
fi
if [ ! "$counter" ] ; then
counter=”10″
fi
}
printrxbytes(){
/sbin/ifconfig “$interface” | grep “RX bytes” | cut -d: -f2 | awk ‘{ print $1 }'
}
printtxbytes(){
/sbin/ifconfig “$interface” | grep “TX bytes” | cut -d: -f3 | awk ‘{ print $1 }'
}
bytestohumanreadable(){
multiplier=”0″
number=”$1″
while [ "$number" -ge 1024 ] ; do
multiplier=$(($multiplier+1))
number=$(($number/1024))
done
case “$multiplier” in
1)
echo “$number Kb”
;;
2)
echo “$number Mb”
;;
3)
echo “$number Gb”
;;
4)
echo “$number Tb”
;;
*)
echo “$1 b”
;;
esac
}
printresults(){
while [ "$counter" -ge 0 ] ; do
counter=$(($counter – 1))
if [ "$rxbytes" ] ; then
oldrxbytes=”$rxbytes”
oldtxbytes=”$txbytes”
fi
rxbytes=$(printrxbytes)
txbytes=$(printtxbytes)
if [ "$oldrxbytes" -a "$rxbytes" -a "$oldtxbytes" -a "$txbytes" ] ; then
echo “$(/bin/date +%Y%m%d-%H%M%S) RXbytes = $(bytestohumanreadable $(($rxbytes – $oldrxbytes))) TXbytes = $(bytestohumanreadable $(($txbytes – $oldtxbytes)))”
else
echo “Monitoring $interface every $sleep seconds. (RXbyte total = $(bytestohumanreadable $rxbytes) TXbytes total = $(bytestohumanreadable $txbytes))”
fi
sleep “$sleep”
done
}
readargs “$@”
checkargs
printresults
測(cè)試如下:
每三秒的流量,總輸出999行,可以輸出到文件里,其中:前面為時(shí)間,Rx Packets 是接收數(shù)據(jù)包,即下載,Tx Packets 是發(fā)送數(shù)據(jù)包,即上傳.
[root@host]#sh t.sh -c 999 Monitoring eth0 every 3 seconds. (RXbyte total = 6 Tb TXbytes total = 5 Tb)
20101105-201539 RXbytes = 126 Kb TXbytes = 658 Kb
20101105-201542 RXbytes = 87 Kb TXbytes = 487 Kb
20101105-201545 RXbytes = 159 Kb TXbytes = 668 Kb
20101105-201548 RXbytes = 107 Kb TXbytes = 725 Kb
20101105-201551 RXbytes = 110 Kb TXbytes = 704 Kb
20101105-201554 RXbytes = 90 Kb TXbytes = 726 Kb
20101105-201558 RXbytes = 100 Kb TXbytes = 850 Kb
20101105-201601 RXbytes = 102 Kb TXbytes = 703 Kb
20101105-201604 RXbytes = 168 Kb TXbytes = 693 Kb
20101105-201607 RXbytes = 105 Kb TXbytes = 730 Kb
20101105-201610 RXbytes = 133 Kb TXbytes = 711 Kb
20101105-201613 RXbytes = 431 Kb TXbytes = 703 Kb
20101105-201616 RXbytes = 84 Kb TXbytes = 527 Kb
20101105-201619 RXbytes = 239 Kb TXbytes = 825 Kb
20101105-201622 RXbytes = 117 Kb TXbytes = 801 Kb
20101105-201625 RXbytes = 99 Kb TXbytes = 913 Kb
20101105-201628 RXbytes = 89 Kb TXbytes = 322 Kb
20101105-201631 RXbytes = 63 Kb TXbytes = 73 Kb
20101105-201634 RXbytes = 84 Kb TXbytes = 191 Kb
20101105-201637 RXbytes = 174 Kb TXbytes = 481 Kb
20101105-201640 RXbytes = 120 Kb TXbytes = 383 Kb
20101105-201643 RXbytes = 94 Kb TXbytes = 496 Kb
20101105-201646 RXbytes = 108 Kb TXbytes = 340 Kb
20101105-201649 RXbytes = 91 Kb TXbytes = 639 Kb
20101105-201652 RXbytes = 106 Kb TXbytes = 629 Kb
20101105-201655 RXbytes = 125 Kb TXbytes = 496 Kb
20101105-201658 RXbytes = 90 Kb TXbytes = 537 Kb
20101105-201701 RXbytes = 114 Kb TXbytes = 641 Kb
相關(guān)文章
Shell腳本中$符號(hào)的嵌套使用方法小結(jié)
在編寫(xiě)Shell腳本時(shí),通過(guò)嵌套使用$符號(hào),間接獲取變量或表達(dá)式的值的具體方法,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-05-05
Shell腳本實(shí)現(xiàn)獲取網(wǎng)頁(yè)快照并生成縮略圖
這篇文章主要介紹了Shell腳本實(shí)現(xiàn)獲取網(wǎng)頁(yè)快照并生成縮略圖,本文獲取網(wǎng)頁(yè)快照使用phantomjs、生成縮略圖使用ImageMagick,需要的朋友可以參考下2015-02-02
Linux通過(guò)命令壓縮與解壓縮的方法| tar, tar.gz, tar.bz2
這篇文章主要介紹了Linux通過(guò)命令壓縮與解壓縮的方法| tar, tar.gz, tar.bz2,內(nèi)容囊括了打包、壓縮、解壓縮的基本概念,最后通過(guò)實(shí)例講解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-04-04
關(guān)于shell的幾個(gè)不為人知卻十分有用的命令分享
這篇文章主要介紹了關(guān)于shell的幾個(gè)不為人知卻十分有用的命令,需要的朋友可以參考下2016-03-03
shell腳本無(wú)密碼登錄 expect的使用方法詳解
這篇文章主要介紹了shell腳本無(wú)密碼登錄 expect的使用方法詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10
Linux中執(zhí)行shell腳本的4種方法總結(jié)
這篇文章主要介紹了Linux中執(zhí)行shell腳本的4種方法總結(jié),即在Linux中運(yùn)行shell腳本的4種方法,需要的朋友可以參考下2014-08-08

