#!/usr/bin/Envpython#-*-coding=utf-8-*-#Using GPL v2.7#Author: [email protected]126. Com#python Monitor network card traffic"""1, the realization principle: obtains the system information through the SNMP protocol, then carries on the corresponding computation and the format, the final output result2, special note: SNMP is required on the monitored machine.Yum Install-Y net-snmp*installation"""#!/usr/bin/pythonimport reimport os#get SNMP-MIB2 of the Devicesdef Getallitems (host,oid): SN1= Os.popen ('snmpwalk-v 2c-c Public'+ Host +' '+ OID). Read ().Split('\ n')[:-1] return SN1 #get Network Devicedef getDevices (Host): Device_mib= Getallitems (Host,'RFC1213-MIB::IFDESCR') Device_list= [] forIteminchDevice_mib:ifRe.search ('ETH', Item): Device_list.append (item.Split(':')[3].strip ()) return device_list #get NetworkDatedef getDate (host,oid): Date_mib= Getallitems (host,oid) [1:] Date= [] forIteminchDate_mib:byte=float(item.Split(':')[3].strip ())Date. Append (Str (round (byte/1024x768,2)) +'KB') returnDate if__name__ = ='__main__': Hosts= ['192.168.10.1','192.168.10.2'] forHostinchhosts:device_list=GetDevices (Host) Inside= GetDate (Host,'if-mib::ifinoctets') outside= GetDate (Host,'if-mib::ifoutoctets') Print'=========='+ Host +'==========' forIinchRange (len (inside)): Print'%s:rx:%-15s TX:%s'%(Device_list[i], inside[i], outside[i]) print
Python operations script Python monitors network card traffic