使用shell監控網路即時資料流量

來源:互聯網
上載者:User
直接上代碼:
#!/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 

 

輸出:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.