直接上代码:
#!/bin/bash function usage { echo "Usage: $0 " echo "e.g. $0 eth0 2" exit 1 } if [ $# -lt 2 ];then usage fi eth=$1 interval=$2 in_old=$(cat /proc/net/dev | grep -w $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{ print $1 }' ) out_old=$(cat /proc/net/dev | grep -w $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{ print $9 }' ) while true do sleep ${interval} in=$(cat /proc/net/dev | grep -w $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{ print $1 }' ) out=$(cat /proc/net/dev | grep -w $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk '{ print $9 }') sub_in=$(( ($in-$in_old)/$interval )) sub_out=$(( ($out-$out_old)/$interval )) echo "Recv rate: $((${sub_in}/1024)) KB/s Sent rate: $((${sub_out}/1024)) KB/s " in_old=${in} out_old=${out} done exit 0
输出:
Recv rate: 1175 KB/s Sent rate: 29 KB/s
Recv rate: 1175 KB/s Sent rate: 29 KB/s
Recv rate: 1179 KB/s Sent rate: 31 KB/s
Recv rate: 987 KB/s Sent rate: 33 KB/s
Recv rate: 1159 KB/s Sent rate: 29 KB/s
Recv rate: 1167 KB/s Sent rate: 29 KB/s
Recv rate: 1082 KB/s Sent rate: 27 KB/s
Recv rate: 1085 KB/s Sent rate: 27 KB/s
Recv rate: 1129 KB/s Sent rate: 29 KB/s
Recv rate: 954 KB/s Sent rate: 24 KB/s
Recv rate: 465 KB/s Sent rate: 18 KB/s
Recv rate: 507 KB/s Sent rate: 14 KB/s
Recv rate: 1135 KB/s Sent rate: 30 KB/s
与dstat的对比:dstat -n -N peth1
1176k 30k
1176k 30k
1180k 31k
987k 33k
1159k 29k
1167k 30k
1083k 27k
1085k 27k
1129k 29k
955k 24k
466k 19k
508k 15k
1135k 30k