网卡流量监控脚本 ( Shell )

时间:2022-08-14 13:31:59
#!/bin/bash
# Traffic Monitor
# author: Xiao Guaishou get_traffic_info(){ recv=`cat /proc/net/dev | awk -F '[: ]+' '/'"$dev"'/{print $3}'`
sent=`cat /proc/net/dev | awk -F '[: ]+' '/'"$dev"'/{print $11}'` } get_traffic_rate(){ In=`echo $[($recv - $old_recv) / ]`
Out=`echo $[($sent - $old_sent) / ]` } read -p '请输入你要监控的网卡设备名:' dev if ! grep -qP "\b$dev\b:" /proc/net/dev
then
echo "你输入的设备不存在,或不是合法的网卡设备"
exit
fi while :;
do
get_traffic_info old_recv=$recv
old_sent=$sent sleep get_traffic_info get_traffic_rate echo -e "$dev\nInput:\t$In KB/s\nOutput:\t$Out KB/s\n" done # End