查看Linux服务器网卡流量小脚本shell和Python各一例
生活随笔
收集整理的這篇文章主要介紹了
查看Linux服务器网卡流量小脚本shell和Python各一例
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? 有時(shí)我們需要較為實(shí)時(shí)的查看服務(wù)器上的網(wǎng)卡流量,這里我寫了兩個(gè)小腳本,一個(gè)用shell(先寫的,一次只能查看一個(gè)網(wǎng)卡),另一個(gè)用Python(后寫的,一次可查看多個(gè)網(wǎng)卡)。腳本中都用了while true“死循環(huán)”,每隔10s從“/proc/net/dev”中取一次值并根據(jù)10s內(nèi)的差值計(jì)算10s內(nèi)的平均帶寬;按ctrl+c停止執(zhí)行。腳本兼容centos6和7
兩個(gè)腳本都不太復(fù)雜,而且腳本中注釋也比較細(xì)致,所以我就不過多解釋腳本內(nèi)容了。直接上圖上腳本:
shell版--使用截圖:
shell版代碼:
#!/bin/sh #by?ljk?20160526if?[?"$1"?=?""?];then????#判斷后面是否有跟參數(shù)echo?-e?"\n??????use?interface_name?after?the?script,like?\"script?eth0\"...\n"exit?-1 fiecho?-e?"\n??????start?monitoring?the?$1,press?\"ctrl+c\"?to?stop" echo?----------------------------------------------------------file=/proc/net/dev????#內(nèi)核網(wǎng)卡信息文件 while?truedoRX_bytes=`cat?$file|grep?$1|sed?'s/^?*//g'|awk?-F'[?:]+'?'{print?$2}'`????#這里sed這一步為了同時(shí)兼容centos6和7TX_bytes=`cat?$file|grep?$1|sed?'s/^?*//g'|awk?-F'[?:]+'?'{print?$10}'`sleep?10RX_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=Mbspeed_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 donePython版--使用截圖:
Python版代碼:
#!/bin/env?python3 #by?ljk?20160526import?os,re,sys,timeif?len(sys.argv)?==?1:print('\n使用方法:請(qǐng)跟上網(wǎng)卡名稱,可接"單個(gè)網(wǎng)卡"/"多個(gè)網(wǎng)卡,以空格分開".\n')sys.exit(100) else:print('start?monitoring,press?"ctrl+c"?to?stop\n')for?arg?in?sys.argv[1:]:????#輸出標(biāo)頭header?=?'------{}?bandwidth(Mb/s)------'.format(arg)print(header.ljust(35),end='')print()#global?values_dicvalues_dic?=?{}????#定義空字典,用來在下面函數(shù)中存放各網(wǎng)卡的各項(xiàng)需要用到的值def?get_values(orders):try:with?open('/proc/net/dev')?as?f:lines=f.readlines()????#內(nèi)容不多,一次性讀取較方便for?arg?in?sys.argv[1:]:for?line?in?lines:line=line.lstrip()????#去掉行首的空格,以便下面splitif?re.match(arg,line):values?=?re.split("[?:]+",line)????#以空格和:作為分隔符values_dic[arg+'r'+orders]=values[1]????#1為接收值values_dic[arg+'t'+orders]=values[9]????#9為發(fā)送值#return?[values[1],values[9]]????#可返回列表except?(FileExistsError,FileNotFoundError,PermissionError):print('open?file?error')sys.exit(-1)try:while?True:get_values('first')????#第一次取值time.sleep(10)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*8t_bandwidth?=?(int(values_dic[arg+'t'+'second'])?-?int(values_dic[arg+'t'+'first']))/1024/1024/10*8print('IN:?'+str(round(r_bandwidth,2)).ljust(8)+'??OUT:?'+str(round(t_bandwidth,2)).ljust(16),end='')print()values_dic?=?{}????#清空本次循環(huán)后字典的內(nèi)容except?KeyboardInterrupt:print("\n-----bye-----")這倆腳本使用起來都還是很方便實(shí)用的,共享出來希望能給朋友們工作中帶來一點(diǎn)方便。
轉(zhuǎn)載于:https://blog.51cto.com/kaifly/1783530
總結(jié)
以上是生活随笔為你收集整理的查看Linux服务器网卡流量小脚本shell和Python各一例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Azure手把手系列 2:微软中国云服务
- 下一篇: mysql定期备份