Sometimes we need a more real-time view of the network card traffic on the server, here I wrote two small scripts, one with the shell (first write, only one network card can be viewed at a time), the other with Python (after the write, one can view multiple network cards). 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 compatible with CENTOS6 and 7
Two scripts are not too complicated, and the annotations in the script are more detailed, so I don't have much to explain the script content. Directly on the script:
Shell version-using:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/81/FD/wKiom1dGtVjC0q0TAABf9C5ErSY496.png "title=" 1.png " alt= "Wkiom1dgtvjc0q0taabf9c5ersy496.png"/>
Shell Version Code:
#!/bin/sh#by ljk 20160526if [ "$" = " ];then #" Determine if the following parameters echo -e "\n use interface_ Name after the script,like \ "script eth0\" ... \ n " exit - 1fiecho -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
Python version-using:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/81/FD/wKiom1dGuoqS6honAAA8gQMh4e8068.png "title=" 2.png " alt= "Wkiom1dguoqs6honaaa8gqmh4e8068.png"/>
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/81/FC/wKioL1dGu5HDfYfbAABaNXTdogo428.png "title=" 3.png " alt= "Wkiol1dgu5hdfyfbaabanxtdogo428.png"/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/81/FC/wKioL1dGu6DRv4BPAAEkqYfpne0398.png "title=" 4.png " alt= "Wkiol1dgu6drv4bpaaekqyfpne0398.png"/>
Python Version code:
#!/bin/env python3#by ljk 20160526import os,re,sys,timeif len (SYS.ARGV) == 1: print (' \ n use: Please keep up with the name of the network card, you can connect the "single network Card"/"Multiple network cards, separated by a space". \ n ') sys.exit (100 ) else: print (' start monitoring,press ' CTRL + C ' to stop\n ') for arg in sys.argv[1:]: #输出标头 header = '------{} bandwidth (MB/s)------'. Format (ARG) print (Header.ljust (+), end= ") print () #global values_dic values_dic = {} #定义空字典, The value to be used to store each NIC in the following function def get_values (orders): try: with open ('/proc/ Net/dev ') aS f: lines =f.readlines () #内容不多, easy to read at once for arg in sys.argv[1:]: for line in lines: line=line.lstrip () #去掉行首的空格, So that the following split if re.match (Arg,line): values&Nbsp;= re.split ("[ :]+", line) #以空格和: as separators values_dic[arg+ ' R ' +orders]=values[1] #1为接收值 values_dic[arg+ ' t ' +orders]=values[9] #9为发送值 #return [values[1],values[9]] #可返回列表 except (Fileexistserror, Filenotfounderror,permissionerror): Print (' Open file error ') &nBsp; sys.exit ( -1) try: while True: get_values (' first ') #第一次取值 time.sleep (Ten) get_values (' second ') #10s后第二次取值 for arg in sys.argv[1:]: r_bandwidth = (int (values_dic[arg+ ' r ' + ') Second ']) - int (values_dic[arg+ ' r ' + ' first '))/1024/1024/10*8 t_bandwidth = (int (values_dic[arg+ ' t ' + ') Second ']) - int (VAlues_dic[arg+ ' t ' + ' first '))/1024/1024/10*8 print (' in: ' +str (Round (r_bandwidth,2)). Ljust (8) + ' OUT: ' + Str (round (t_bandwidth,2)). Ljust (+), end= ') print () values_dic = {} #清空本次循环后字典的内容 except KeyboardInterrupt: print ("\ n-----Bye-----")
These two scripts are still very convenient and practical, sharing out hope to give friends a little convenience in their work.
This article is from "Endeavor K" blog, please be sure to keep this source http://kaifly.blog.51cto.com/3209616/1783530
View Linux Server NIC traffic small script shell and Python example