1

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.

blackbrayn
  • 162
  • 1
  • 6

2 Answers2

0

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
PSS
  • 5,561
  • 5
  • 28
  • 30
  • this works , but only for the first occurence , in my case the number of mac addresses is variable between 1 and as many as 30 and each one must be compared with a existing list of macs. – blackbrayn May 31 '13 at 15:58
  • for multiple occurrences you gotta do re.findall(). However, you only have single mac per line. So, you could loop through your file line by line and search for MAC addresses this way. I'll update my answer shortly. – PSS May 31 '13 at 19:05
  • thx for the answer , i do apreciate the help but in your example you read the lines from a file , i need to get the "matching" from the string i get as output by running the command in the shell - "output". – blackbrayn May 31 '13 at 19:53
  • you can do output.split('\n') to get the same result :) – PSS May 31 '13 at 20:38
  • i couldn't make it work give out an error:mac = re.search(strToFind , line).group() AttributeError: 'NoneType' object has no attribute 'group' – blackbrayn Jun 05 '13 at 13:38
  • I fixed the code above. The error is due to the fact that re.search() returns NoType if nothing is found. if-statement takes care of it. We only group() result if there is result :) – PSS Jun 05 '13 at 16:16
  • it won't do the matching , i printed the line and it gives me the lines but if i try to print results , right after "results = re.search(strToFind, line)" i get a lot of "none" , i'll try to dig deeper. – blackbrayn Jun 05 '13 at 18:54
  • i did a response , managed to make it work , thx a lot for the help. – blackbrayn Jun 05 '13 at 18:54
0

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)
olyv
  • 3,699
  • 5
  • 37
  • 67
vict
  • 1