Shell中怎么實現(xiàn)單機流量統(tǒng)計功能-創(chuàng)新互聯(lián)

本篇文章為大家展示了Shell中怎么實現(xiàn)單機流量統(tǒng)計功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

你所需要的網(wǎng)站建設(shè)服務(wù),我們均能行業(yè)靠前的水平為你提供.標(biāo)準(zhǔn)是產(chǎn)品質(zhì)量的保證,主要從事做網(wǎng)站、成都網(wǎng)站設(shè)計、企業(yè)網(wǎng)站建設(shè)、成都手機網(wǎng)站制作、網(wǎng)頁設(shè)計、成都品牌網(wǎng)站建設(shè)、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。創(chuàng)新互聯(lián)建站擁有實力堅強的技術(shù)研發(fā)團隊及素養(yǎng)的視覺設(shè)計專才。

#!/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


測試如下:

每三秒的流量,總輸出999行,可以輸出到文件里,其中:前面為時間,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


上述內(nèi)容就是Shell中怎么實現(xiàn)單機流量統(tǒng)計功能,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前題目:Shell中怎么實現(xiàn)單機流量統(tǒng)計功能-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://bm7419.com/article24/ipcce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作軟件開發(fā)、云服務(wù)器建站公司、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計