linux - How can I parse the output of /proc/net/dev into key:value pairs per interface using Python? -


The output of Linux looks like this:

  inter- Find | Transmitted face | Bytes packet errs Drop FIFO frame compressed multicast | Bytes packets errs drop fifo colls carriers have narrowed: 18748525 129811 0 0 0 0 0 0 18748525 129811 0 0 0 0 0 0 eth0: 1699369069 226296437 0 0 0 0 0 3555 4118745424 194001149 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0  

How can I use Python to parse this output into the key : Add value to each interface? I got it to use open scripting and there, but I need to use the python.

This is very streamlined input and you can easily get columns and data lists by splitting each line , And then make a word about it.

There is a simple script without regex

  line = open ("/ proc / net / dev", "r"). Readline () column line = line [1] _, received call, transmitcales = columnline. ("|") Receive = Map (Lambda A: "Recovery_" + A, receiveCols.split ()) transmitCols = map (Lambda A: "trans_" + a, transmitCols.split ()) cols = receiveCols + transmitCols faces = {} Lines for line [2:]: Find the line (":") & lt; 0: face continue, data = line. Split ( ":") faceData = dict (ZIP, cols, data.split ()) face [face] = Fesdata import Piprint pprint.pprint (face)  

Take this output

  { '': { 'recv_bytes': '7056295', 'recv_compressed': '0', 'recv_drop': '0', 'recv_errs' '0', 'Rakw_fifo' '0', 'Rakw_frem' '0', 'Rakw_maltikast' '0', 'Rakw_packets': '12148', 'Trons_bits ":" 7056295 "," Trons_krriar' '0', 'Trons_cols'' 0 ',' Trons_kanpresd '' 0 ',' Trons_drap '' 0 ',' Trons_arrs' '0', 'Trons_fifo' '0', 'Trons_packets'' 12148 '}' Ath0 ': {' Rakw_bits ': '34, 084,530', 'Rakw_kanpresd' '0', 'Rakw_drap' '0', 'Rakw_arrs' '0', 'Rakw_fifo' '0'; 'Recv_frame': '0', 'rw_multicast': '0', 'rick-packets':' 3059 9 ',' trans_bytes': '6,170,441', 'tran '0', 'trans_colls':' 0 ',' trans_compressed ':' 0 ',' trans_drop ':' 0 ',' trans_errs': '0', 'Trans_fifo': ​​'0', ' Trans_packets': '32377'}}  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -