Sometimes we need to look at the network card traffic on the server in real time, here I write a shell script. The script uses a while true "dead loop", every 10s from "/proc/net/dev" to take a value and calculates the average bandwidth within 10s based on the difference within 10s; Press CTRL + C to stop execution. Scripts are compatible with CENTOS6 and 7, the scripts are not too complex, and the annotations in the script are more nuanced, so I don't have to explain the script much.
Note: 1kb=8 bytes, 1mb=1024kb
#!/bin/sh if [ " = " ];then #判断后面是否有跟参数 echo -e "\n use interface_name after The script,like \ "script eth0\" ... \ n " exit -1fi echo -e "\n start monitoring the $1,press \" ctrl+c\ " to stop "echo ---------------------------------------------------------- file=/proc/net/ dev #内核网卡信息文件while true do rx_bytes= ' cat $file |grep $1|sed ' s/^ *//g ' |awk -f ' [ :]+ ' ' {print $2} ' #这里sed这一步为了同时兼容centos6和7 tx_bytes= ' cat $file |grep $1|sed ' s/^ *//g ' |awk -f ' [ :]+ ' ' {print $10} ' sleep 10 rx_byteS_later= ' cat $file |grep $1|sed ' s/^ *//g ' |awk -f ' [ :]+ ' ' {print $2} ' tx_bytes_later= ' cat $file |grep $1|sed ' s/^ *//g ' |awk -F ' [ :]+ ' ' {print $10} ' #B *8/1024/1024=mb speed_rx= ' echo ' scale=2 ($RX _bytes_later - $RX _bytes) *8/1024/1024/10 "|BC" speed_tx= ' echo ' scale=2 ($TX _bytes_later - $TX _bytes) *8/1024/1024/10 "|BC" printf "%-3s %-3.1f %-10s %-4s %-3.1f %-4s\n" in: $ speed_rx mb/s out: $speed _tx mb/sdone
This article is from the "burning Years of Passion" blog, please be sure to keep this source http://liuzhengwei521.blog.51cto.com/4855442/1786936
View Linux Server NIC traffic small script shell