Created
December 28, 2017 18:41
-
-
Save paulc/cde00de9d9d6b82ca5903064022e5f9d to your computer and use it in GitHub Desktop.
ifconfig.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re,subprocess | |
def ifconfig(): | |
ifconfig = {} | |
dev = None | |
for l in subprocess.Popen('ifconfig',stdout=subprocess.PIPE).stdout: | |
l = l.decode('ascii').rstrip() | |
m = re.match('^([a-z]+[0-9]+): flags=(.*?) (.*)',l) | |
k = v = None | |
if m: | |
dev,flags,rest = m.groups() | |
v = rest.split() | |
ifconfig[dev] = {'flags':flags} | |
ifconfig[dev].update(zip(v[::2],v[1::2])) | |
elif dev: | |
m = re.match('\s+(\w+)[:= ]+(.*)',l) | |
if m: | |
k,v = m.groups() | |
if k in ('description','ether','groups'): | |
ifconfig[dev][k] = v | |
elif k in ('inet','inet6'): | |
v = l.split() | |
addr = v[1] | |
params = dict(zip(v[::2],v[1::2])) | |
ifconfig[dev][k] = ifconfig[dev].get(k,{}) | |
ifconfig[dev][k][addr] = params | |
elif k in ('member'): | |
ifconfig[dev][k] = ifconfig[dev].get(k,[]) + [v] | |
return ifconfig | |
if __name__ == '__main__': | |
import pprint | |
pprint.pprint(ifconfig()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment