实时监测网卡的上传下载速度的Linux小脚本

#!/bin/sh

###统计周期内的平均上传下载速度,以Mb为单位
if [ -z "$1" -o -z "$2" ];then
  echo -e "\nUsage: $0    eg:$0 eth0 5\n"
  exit -1
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更新一次数据

你可能感兴趣的:(linux,运维管理)