码迷,mamicode.com
首页 > Web开发 > 详细

解析/proc/net/dev

时间:2018-02-01 20:47:04      阅读:1358      评论:0      收藏:0      [点我收藏+]

标签:question   href   readlines   drop   readline   ack   eve   linux   sed   

https://stackoverflow.com/questions/1052589/how-can-i-parse-the-output-of-proc-net-dev-into-keyvalue-pairs-per-interface-u

https://www.ibm.com/developerworks/cn/linux/1312_caojh_pythonlinux/index.html

#!/usr/bin/python

lines = open("/proc/net/dev", "r").readlines()

columnLine = lines[1]
_, receiveCols , transmitCols = columnLine.split("|")

receiveCols = map(lambda a:"recv_"+a, receiveCols.split())
transmitCols = map(lambda a:"trans_"+a, transmitCols.split())
cols = receiveCols+transmitCols

faces = {}
for line in lines[2:]:
    if line.find(":") < 0: continue
    face, data = line.split(":")
    faceData = dict(zip(cols, data.split()))
    faces[face.strip()] = faceData

import pprint
pprint.pprint(faces.get(‘bond0‘,‘‘))

输出结果:

{‘recv_bytes‘: ‘57977578623256‘,
 ‘recv_compressed‘: ‘0‘,
 ‘recv_drop‘: ‘5662‘,
 ‘recv_errs‘: ‘0‘,
 ‘recv_fifo‘: ‘0‘,
 ‘recv_frame‘: ‘0‘,
 ‘recv_multicast‘: ‘83657344‘,
 ‘recv_packets‘: ‘255653410912‘,
 ‘trans_bytes‘: ‘460049069676059‘,
 ‘trans_carrier‘: ‘0‘,
 ‘trans_colls‘: ‘0‘,
 ‘trans_compressed‘: ‘0‘,
 ‘trans_drop‘: ‘0‘,
 ‘trans_errs‘: ‘0‘,
 ‘trans_fifo‘: ‘0‘,
 ‘trans_packets‘: ‘315175191578‘}

  

解析/proc/net/dev

标签:question   href   readlines   drop   readline   ack   eve   linux   sed   

原文地址:https://www.cnblogs.com/standby/p/8392803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!