View Linux Server NIC traffic small script shell

Source: Internet
Author: User

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

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.