监控网卡流量python代码
2018-07-20 来源:open-open
[Python]代码
import re,time def get_net_info(): flow1 = open('/proc/net/dev') lines = flow1.read() flow1.close() e = re.compile('(eth.)') r_re = re.compile('eth..(.*?\s)') r_tr = re.compile(".*eth.*") match_re = r_re.findall(lines) match_tr = r_tr.findall(lines) eth = e.findall(lines) return (eth,lines,match_re,match_tr) def net_flow(): net_re_c={} net_tr_c={} net_tr_a=[] for i in get_net_info()[3]: net_tr_a.append(i.split()[8]) net_tr_a=dict(zip(get_net_info()[0],net_tr_a)) net_re_a = dict(zip(get_net_info()[0],get_net_info()[2])) print "{eth0:(transmit,receive)" while True: time.sleep(1) net_re_b = dict(zip(get_net_info()[0],get_net_info()[2])) net_tr_b = [] for i in get_net_info()[3]: net_tr_b.append(i.split()[8]) net_tr_b=dict(zip(get_net_info()[0],net_tr_b)) for i in net_re_b: net_re_c[i] = int((float(net_re_b[i])-float(net_re_a[i]))/1024) for i in net_tr_b: net_tr_c[i]=int((float(net_tr_b[i])-float(net_tr_a[i]))/1024) net_re_a = net_re_b net_tr_a = net_tr_b net_flow={} for a in net_re_c: net_flow[a]=(net_tr_c[a],net_re_c[a]) print net_flow if __name__ == "__main__": net_flow()
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐