本脚本是无意间看到网络大神编写发表的,未留存原链接,感谢提供,侵删。

实现原理:通过每秒读取一次/proc/net/dev动态文件,计算得出平局速度。执行格式

sh ./speedmonitor.sh eth0

脚本详情:

#!/bin/bash

ethn=$1

while true
do
 RX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
 sleep 1
 RX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')

 clear
 echo -e "\t RX `date +%k:%M:%S` TX"

 RX=$((${RX_next}-${RX_pre}))
 TX=$((${TX_next}-${TX_pre}))

 if [[ $RX -lt 1024 ]];then
 RX="${RX}B/s"
 elif [[ $RX -gt 1048576 ]];then
 RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
 else
 RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
 fi

 if [[ $TX -lt 1024 ]];then
 TX="${TX}B/s"
 elif [[ $TX -gt 1048576 ]];then
 TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
 else
 TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
 fi

 echo -e "$ethn \t $RX $TX "

done
12-16 21:27