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.

4

2 回答 2

0

你可以用正则表达式过滤掉这样的mac地址:

>>> 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'

您可以查看新找到的 MAC 地址是否已经在这样的列表中:

>>> 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

添加:要每行查找单个匹配项:

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
于 2013-05-30T19:05:11.627 回答
0

上面给定的正则表达式["strToFind = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', re.I)"]将匹配最后一个八位字节中的无效值,如下所示'00:14:5e:6b:4b:eah'

所以对正则表达式稍作改动,只需用'$'结束最后一个八位字节。像这样:

strToFind = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2}$)', re.I)
于 2014-06-26T17:14:32.430 回答