Monitor real-time network traffic using shell

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.