#!/bin/sh
 
###统计周期内的平均上传下载速度，以Mb为单位
if [ -z "$1" -o -z "$2" ];then
  echo -e "\nUsage: $0 <if> <cycle>  eg:$0 ens5 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
