Monitor linux traffic for python and linux traffic for python

Source: Internet
Author: User

Monitor linux traffic for python and linux traffic for python

Monitor linux traffic in python

Directly upload the code and use OptionParser to input parameters.

#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)

Usage:

''' % Prog [-I ethname] [-t interveltime] Example: % prog-I eth0-t 1 '''
The default value is eth0 and the interval is 1.


If you have any questions, please contact 362299908@qq.com

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.