天天看點

實時監測網卡的上傳下載下傳速度的Linux小腳本

#!/bin/sh

###統計周期内的平均上傳下載下傳速度,以Mb為機關
if [ -z "$1" -o -z "$2" ];then
  echo -e "\nUsage: $0 <if> <cycle>  eg:$0 eth0 5\n"
  exit -
fi

echo -e "\nMonitoring the $1 net flow,press \"ctrl+c\" to stop"
echo ----------------------------------------------------------

while true
  do

  RX_bytes=`cat /proc/net/dev|grep "$1"|awk '{print $2}'`
  TX_bytes=`cat /proc/net/dev|grep "$1"|awk '{print $10}'`

  sleep $2

  RX_bytes_later=`cat /proc/net/dev|grep "$1"|awk '{print $2}'`
  TX_bytes_later=`cat /proc/net/dev|grep "$1"|awk '{print $10}'`


  #Mb=B*8/1024/1024
  speed_RX=`echo $(expr "scale=2;($RX_bytes_later - $RX_bytes)*8/1024/1024/$2"|bc)`
  speed_TX=`echo $(expr "scale=2;($TX_bytes_later - $TX_bytes)*8/1024/1024/$2"|bc)`

  printf "Download: %.2f Mb/s\tUpload: %.2f Mb/s\n" $speed_RX  $speed_TX 

done
           

使用示例:

./net_flow eth0 5 #監測網卡eth0的速度,每隔5s更新一次資料

繼續閱讀