Directly run the Code:
#!/bin/bashfunction usage{ echo "Usage: $0 " echo "e.g. $0 eth0 2" exit 1}if [ $# -lt 2 ];then usagefieth=$1interval=$2in_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 truedosleep ${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/sSent rate: $((${sub_out}/1024)) KB/s "in_old=${in}out_old=${out}doneexit 0
Output:
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
Comparison with dstat: dstat-n peth1
1176 K 30 K
1176 K 30 K
1180 K 31 K
987 k 33 K
1159 K 29 K
1167 K 30 K
1083 K 27 k
1085 K 27 k
1129 K 29 K
955 K 24 K
466 K 19 k
508 K 15 K
1135 K 30 K