#!/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) / 1024]` Out=`echo $[($sent - $old_sent) / 1024]` } read -p '请输入你要监控的网卡设备名:' dev if ! grep -qP "\b$dev\b:" /proc/net/dev then echo "你输入的设备不存在,或不是合法的网卡设备" exit 1 fi while :; do get_traffic_info old_recv=$recv old_sent=$sent sleep 1 get_traffic_info get_traffic_rate echo -e "$dev\nInput:\t$In KB/s\nOutput:\t$Out KB/s\n" done # End