Sometimes we need a more real-time view of the network card traffic on the server, here I wrote two small script, one with the shell (first, only one network card can be viewed at a time), another in Python (after writing, can view more than one network card).
In the script all use the while true "dead loop", every 10s from "/proc/net/dev" to take a value and calculate 10s within 10s according to the difference value of the average bandwidth; Press CTRL + C to stop execution. Script compatible CENTOS6 and 7
The two scripts are not very complex, and the comments in the script are meticulous, so I don't explain the script much more.
Directly above the script:
Shell Version – Use screenshots:
Shell Version Code:
#!/bin/sh #by LJK 20160526 If ["$" = "];then #判断后面是否有跟参数 echo-e" \ n use Inter Face_name after the script,like \ "Script eth0\" ... \ n "exit-1 fi echo-e" \ Start monitoring the $1,press \ "Ctrl+c\"
To stop Echo----------------------------------------------------------File=/proc/net/dev #内核网卡信息文件 the while True Rx_bytes= ' cat $file |grep $1|sed ' s/^ *//g ' |awk-f ' [:]+ ' {print $} ' #这里sed这一步为了同时兼容centos6和7 tx_bytes= ' cat $file |gr EP $1|sed ' s/^ *//g ' |awk-f ':]+ ' {print $} ' sleep rx_bytes_later= ' cat $file |grep $1|sed ' s/^ *//g ' |awk-f ' [: ]+ ' {print $} ' tx_bytes_later= ' cat $file |grep $1|sed ' s/^ *//g ' |awk-f ' [:]+ ' ' {print $} ' #B *8/1024/1024=MB s Peed_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/s done
Python Version – Use screenshots:
Python Version code:
#!/bin/env python3 #by ljk 20160526 Import os,re,sys,time If Len (sys.argv) = = 1:print (' \ n usage: Please follow the network card name, connect to the "single Network Card"/" Multiple network adapters, separated by spaces ". \ n") sys.exit 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 () #gl Obal values_dic values_dic = {} #定义空字典, used to store the values of each NIC in the following function Def get_values (orders): Try:with open (' /proc/net/dev ') as F:lines=f.readlines () #内容不多, more convenient for a one-time read for ARG in sys.argv[1:]: For line I n Lines:line=line.lstrip () #去掉行首的空格 so that the following split if Re.match (arg,line): values = r E.split ("[:]+", line) #以空格和: As separator values_dic[arg+ ' R ' +orders]=values[1] #1为接收值 values_dic[a rg+ ' t ' +orders]=values[9] #9为发送值 #return [values[1],values[9]] #可返回列表 except (fileexistserror,filenot Founderror,permissionerrOR): Print (' Open file Error ') Sys.exit ( -1) try:while true:get_values (' a ') #第一次取值 Time.sleep (a) get_values (' second ') #10s后第二次取值 for Arg in sys.argv[1:]: r_bandwidth = (int (val ues_dic[arg+ ' R ' + ' second '])-int (values_dic[arg+ ' r ' + ' a '))/1024/1024/10*8 t_bandwidth = (int (values_dic[arg+ ') T ' + ' second '])-int (values_dic[arg+ ' t ' + ' a '))/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 Keybo
Ardinterrupt:print ("\ n-----Bye-----")
These two scripts are still very convenient to use and practical, shared out hope to give friends work to bring a little convenience.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.