Note that there are some explanatory texts on larger screens.

plurals
  1. POAlgorithm to extract network info from ifconfig (ubuntu)
    primarykey
    data
    text
    <p>Im trying to parse info from ifconfig (ubuntu). Normally, I would split a chunk of data like this down into words, and then search for substrings to get what I want. For example, given <code>line = "inet addr:192.168.98.157 Bcast:192.168.98.255 Mask:255.255.255.0"</code>, and looking for the broadcast address, I would do:</p> <pre><code>for word in line.split(): if word.startswith('Bcast'): print word.split(':')[-1] &gt;&gt;&gt;192.168.98.255 </code></pre> <p>However, I feel its about time to start learning how to use regular expressions for tasks like this. Here is my code so far. I've hacked through a couple of patterns (inet addr, Bcast, Mask). Questions after code...</p> <pre><code># git clone git://gist.github.com/1586034.git gist-1586034 import re import json ifconfig = """ eth0 Link encap:Ethernet HWaddr 08:00:27:3a:ab:47 inet addr:192.168.98.157 Bcast:192.168.98.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe3a:ab47/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:189059 errors:0 dropped:0 overruns:0 frame:0 TX packets:104380 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:74213981 (74.2 MB) TX bytes:15350131 (15.3 MB)\n\n lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:389611 errors:0 dropped:0 overruns:0 frame:0 TX packets:389611 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:81962238 (81.9 MB) TX bytes:81962238 (81.9 MB) """ for paragraph in ifconfig.split('\n\n'): info = { 'eth_port': '', 'ip_address': '', 'broadcast_address': '', 'mac_address': '', 'net_mask': '', 'up': False, 'running': False, 'broadcast': False, 'multicast': False, } if 'BROADCAST' in paragraph: info['broadcast'] = True if 'MULTICAST' in paragraph: info['multicast'] = True if 'UP' in paragraph: info['up'] = True if 'RUNNING' in paragraph: info['running'] = True ip = re.search( r'inet addr:[^\s]+', paragraph ) if ip: info['ip_address'] = ip.group().split(':')[-1] bcast = re.search( r'Bcast:[^\s]+', paragraph ) if bcast: info['broadcast_address'] = bcast.group().split(':')[-1] mask = re.search( r'Mask:[^\s]+', paragraph ) if mask: info['net_mask'] = mask.group().split(':')[-1] print paragraph print json.dumps(info, indent=4) </code></pre> <p>Here're my questions:</p> <ol> <li><p>Am I taking the best approach for the patterns I have already implemented? Can I grab the addresses without splitting on ':' and then choosing the last of the array.?</p></li> <li><p>I'm stuck on HWaddr. What would be a pattern to match this mac address?</p></li> </ol> <p>EDIT:</p> <p>Ok, so here's how I ended up going about this. I started out trying to go about this without the regex... just manipulating stings and lists. But that proved to be a nightmare. For example, what separates <code>HWaddr</code> from its address is a <code>space</code>. Now take <code>inet addr</code> its separated from its address by <code>:</code>. Its a total bitch to scrape with differing separators like this. Not only a bitch to code but also a bitch to read. </p> <p>So, I did this with regex. I think this makes a strong case for when to use regular expressions.</p> <pre><code># git clone git://gist.github.com/1586034.git gist-1586034 # USAGE: pipe ifconfig into script. ie "ifconfig | python pyifconfig.py" # output is a list of json datastructures import sys import re import json ifconfig = sys.stdin.read() print 'STARTINPUT' print ifconfig print 'ENDINPUT' def extract(input): mo = re.search(r'^(?P&lt;interface&gt;eth\d+|eth\d+:\d+)\s+' + r'Link encap:(?P&lt;link_encap&gt;\S+)\s+' + r'(HWaddr\s+(?P&lt;hardware_address&gt;\S+))?' + r'(\s+inet addr:(?P&lt;ip_address&gt;\S+))?' + r'(\s+Bcast:(?P&lt;broadcast_address&gt;\S+)\s+)?' + r'(Mask:(?P&lt;net_mask&gt;\S+)\s+)?', input, re.MULTILINE ) if mo: info = mo.groupdict('') info['running'] = False info['up'] = False info['multicast'] = False info['broadcast'] = False if 'RUNNING' in input: info['running'] = True if 'UP' in input: info['up'] = True if 'BROADCAST' in input: info['broadcast'] = True if 'MULTICAST' in input: info['multicast'] = True return info return {} interfaces = [ extract(interface) for interface in ifconfig.split('\n\n') if interface.strip() ] print json.dumps(interfaces, indent=4) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload