国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

監控linux流量python版

2019-11-14 16:54:44
字體:
來源:轉載
供稿:網友

python版監控linux流量

直接上代碼,使用OptionParser來傳入參數

#coding:utf-8#-------------#Author:Hu#Data:20150520#-------------from __future__ import divisionimport reimport timefrom optparse import OptionParserdef getbandwidth(eth='eth0',intevel=1):    a=open('/PRoc/net/dev')    data=a.read()    patten=eth + '.*'    if  not re.search(patten,data):        print "The ETHname not have"        exit(1)    Rev_old=re.search(patten,data).group().replace(':',' ').split()[1]    Send_old=re.search(patten,data).group().replace(':',' ').split()[9]    a.close()    while True:        #print intevel        time.sleep(int(intevel))        a=open('/proc/net/dev')        data=a.read()        Rev=re.search(patten,data).group().replace(':',' ').split()[1]        Send=re.search(patten,data).group().replace(':',' ').split()[9]        diff_Rev=int(Rev)-int(Rev_old)        diff_Sen=int(Send)-int(Send_old)        diff_M=diff_Rev*8/1024/1024/int(intevel)        diff_S=diff_Sen*8/1024/1024/int(intevel)        print time.strftime("%Y%m%d %H:%M:%S") + '   The Recevie is  %6.2f Mbps(byte is %d)' % (diff_M,diff_Rev) + '   The Send is  %6.2f Mbps(byte is %d)' % (diff_S,diff_Sen)        Rev_old=Rev        Send_old=Send        a.close()if __name__=='__main__':        import sys    usage='''%prog [-i ethname] [-t interveltime]           Example:%prog -i eth0 -t 1'''    parser=OptionParser(usage=usage,version='2.0_20150602')    parser.add_option('-i','--interface',dest='interface',default='eth0',help='Wann to interface')    parser.add_option('-t','--time',dest='intevel',type='int',default='1',help='The intevel time')    (options,args)=parser.parse_args()    print "The interafce is %s and the intevel time is %d" % (options.interface,options.intevel)    getbandwidth(options.interface,options.intevel)

使用方法:

'''%prog [-i ethname] [-t interveltime]           Example:%prog -i eth0 -t 1'''
默認是eth0 ,時間間隔是1

效果如下:
[root@iZ94nv1rj5tZ tools]# ./bandwidth3.py -versionUsage: bandwidth3.py [-i ethname] [-t interveltime]           Example:bandwidth3.py -i eth0 -t 1bandwidth3.py: error: no such option: -v[root@iZ94nv1rj5tZ tools]# ./bandwidth3.py -i eth1 -t 1The interafce is eth1 and the intevel time is 120151113 16:51:43   The Recevie is    0.01 Mbps(byte is 1876)   The Send is    0.00 Mbps(byte is 154)20151113 16:51:44   The Recevie is    0.02 Mbps(byte is 2156)   The Send is    0.00 Mbps(byte is 202)20151113 16:51:45   The Recevie is    0.08 Mbps(byte is 10482)   The Send is    0.00 Mbps(byte is 202)20151113 16:51:46   The Recevie is    0.02 Mbps(byte is 2014)   The Send is    0.00 Mbps(byte is 202)20151113 16:51:47   The Recevie is    0.02 Mbps(byte is 2612)   The Send is    0.00 Mbps(byte is 202)20151113 16:51:48   The Recevie is    0.09 Mbps(byte is 11394)   The Send is    0.00 Mbps(byte is 202)

 



如有問題,請聯系362299908@QQ.com

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 承德县| 永春县| 双牌县| 乐昌市| 通江县| 香河县| 临汾市| 永昌县| 成武县| 军事| 大丰市| 钦州市| 如皋市| 朝阳市| 安塞县| 长葛市| 高阳县| 炉霍县| 洪泽县| 云梦县| 黄山市| 内乡县| 湘阴县| 贞丰县| 康马县| 岫岩| 兰溪市| 文成县| 廊坊市| 加查县| 宁南县| 锡林郭勒盟| 汶上县| 内黄县| 徐水县| 凌海市| 卫辉市| 广昌县| 台山市| 炉霍县| 新宁县|