Skip to content

Instantly share code, notes, and snippets.

@BenTheHokie
Created May 8, 2015 02:01
Show Gist options
  • Save BenTheHokie/ed7646eaf6e0fd66e5f3 to your computer and use it in GitHub Desktop.
Save BenTheHokie/ed7646eaf6e0fd66e5f3 to your computer and use it in GitHub Desktop.
The mystical magical regex
#!/usr/bin/env python3
import re, os
def getIP():
jargon = os.popen('/sbin/ip -o -4 addr list').read().split('\n')
getip = re.compile('[0-9]+: (?P<conn>[A-Za-z0-9]+)( +)inet (?P<addr>([12]?([0-9])+\.){3}([12]?([0-9])+)/[0-9]{1,2})')
retval = []
for a in jargon:
tmp = getip.search(a)
if tmp:
retval.append(
{
'conn' : tmp.group('conn'),
'addr' : tmp.group('addr')
}
)
return retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment