Question

python version 2.7.3

here is the "code" so far:

import subprocess

p = subprocess.Popen(["pppoe-discovery", "-I", "eth0"], stdout=subprocess.PIPE)
output, err = p.communicate()

print output

This will give a string containing all the pppoe servers discovered

My problem is extracting all mac addresses and compare each one with a predefined list or string.

Even if I could find and print all of them , it is still unclear for me as a beginner to find a solution to compare each to see if it's in the list. After that I'll just cook up some if "condition" and send a email with the non-matching mac-address.

output:

Access-Concentrator: xxxx Service-Name: xxxx

Got a cookie: de 58 08 d0 66 c8 58 15 a0 66 9b b1 02 3f 7c 95 1f 42 00 00

AC-Ethernet-Address: 00:22:33:6b:4b:ee

this is just one of the servers , the list goes on.

Was it helpful?

Solution

You could you regex to filter out mac address like this:

>>> import re
>>> input_string = "Access-Concentrator: xxxx Service-Name: xxxx Got a cookie: de 58 08 d0 66 c8 58 15 a0 66 9b b1 02 3f 7c 95 1f 42 00 00 -------------------------------------------------- AC-Ethernet-Address: 00:14:5e:6b:4b:ee –"
>>> mac = re.search(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', input_string, re.I).group()
>>> mac
'00:14:5e:6b:4b:ee'

You could see if newly found MAC address is already in the list like that:

>>> my_macs = ['00:14:5e:6b:4b:ee','00:14:5e:6b:4b:eb','00:14:5e:6b:4b:ec']
>>> mac in my_macs
True

ADDED:To look for single match per line:

import re

my_macs = ['00:14:5e:6b:4b:ea','00:14:5e:6b:4b:eb','00:14:5e:6b:4b:ec']
mac = ''

strToFind = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', re.I)

for line in output.split('\n'):
    results = re.search(strToFind, line)
    if results:
        mac = results.group()
    if mac not in my_macs:
        print mac

OTHER TIPS

Above given regex ["strToFind = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', re.I)"] will match invalid values in last octet like this '00:14:5e:6b:4b:eah'.

So slight change to the regex, just end the last octet with '$'. Like this:

strToFind = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2}$)', re.I)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top