1. 程式人生 > >解析/proc/net/dev

解析/proc/net/dev

ref quest pen lan con ram byte receive blank

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