从系统报告中提取IP地址

时间:2016-09-16 09:13:55

标签: python regex

我遍历地址条目,使用Python正则表达式提取地址以添加到我的列表中。这是一个示例输入字符串和所需的输出。我该怎么做?

var = """

sw1:FID256:root> ipaddrshow 

CHASSIS
Ethernet IP Address: 10.17.11.10
Ethernet Subnetmask: 255.255.210.0

CP0
Ethernet IP Address: 10.17.11.11
Ethernet Subnetmask: 255.255.210.0
Host Name: cp0
Gateway IP Address: 10.17.48.1

CP1
Ethernet IP Address: 10.17.11.12
Ethernet Subnetmask: 255.255.210.0
Host Name: cp1
Gateway IP Address: 10.17.18.1

sw1:FID256:root>
"""

输出:

List Index 0 “chassis,ip 10.17.11.10 chassis,mask 255.255.210.0”

List Index 1 “cp0,ip 10.17.11.11 cp0,mask 255.255.210.0 cp0,gw 10.17.18.1”

List Index 2 “cp1,ip 10.17.11.12 cp1,mask 255.255.240.0 cp1,gw 10.17.18.1”

2 个答案:

答案 0 :(得分:1)

请参阅提取所有数据的this regex

(?m)^([a-zA-Z0-9]+)(?:\r?\n|\r)Ethernet IP Address: ([\d.]+)(?:\r?\n|\r)Ethernet Subnetmask: ([\d.]+)(?:(?:\r?\n|\r)Host Name: ([a-z\d]+)(?:\r?\n|\r)Gateway IP Address: ([\d.]+))?

详细

  • (?m) - 允许^匹配行开头的多行模式
  • ^ - lime start
  • ([a-zA-Z0-9]+) - 第1组,一个或多个字母数字
  • (?:\r?\n|\r) - 换行
  • Ethernet IP Address: - 文字字符串
  • ([\d.]+) - 第2组,1 +位数和点
  • (?:\r?\n|\r)Ethernet Subnetmask: ([\d.]+) - 与上面类似的模式,其中第3组包含子网掩码
  • (?:(?:\r?\n|\r)Host Name: ([a-z\d]+) - 与上面类似的模式,其中第4组包含主机名
  • (?:\r?\n|\r)Gateway IP Address: ([\d.]+))? - 与第5组包含网关的类似模式。

现在,您只需要使用re.finditer并构建结果字符串:

import re
p = re.compile(r'^([a-zA-Z0-9]+)(?:\r?\n|\r)Ethernet IP Address: ([\d.]+)(?:\r?\n|\r)Ethernet Subnetmask: ([\d.]+)(?:(?:\r?\n|\r)Host Name: ([a-z\d]+)(?:\r?\n|\r)Gateway IP Address: ([\d.]+))?', re.MULTILINE)
s = "sw1:FID256:root> ipaddrshow \n\nCHASSIS\nEthernet IP Address: 10.17.11.10\nEthernet Subnetmask: 255.255.210.0\n\nCP0\nEthernet IP Address: 10.17.11.11\nEthernet Subnetmask: 255.255.210.0\nHost Name: cp0\nGateway IP Address: 10.17.48.1\n\nCP1\nEthernet IP Address: 10.17.11.12\nEthernet Subnetmask: 255.255.210.0\nHost Name: cp1\nGateway IP Address: 10.17.18.1\n\nsw1:FID256:root>"
result = ["{0},ip {1} {0},mask {2} {3},gw {4}".format(z.group(1).lower(),z.group(2),z.group(3).lower(),z.group(4),z.group(5)) if z.group(4) else "{0},ip {1} {0},mask {2}".format(z.group(1).lower(),z.group(2),z.group(3)) for z in p.finditer(s)]
print(result)

请参阅Python demo

答案 1 :(得分:0)

我有类似的东西,并使用字典覆盖任何属性:

(?P<name>^[A-Z0-9]+)\n|(?P<attr>^[\w]+[^:]+):\s(?P<val>[\d\w\.]+)\n

在python中,不可能从每个正则表达式匹配多次匹配的组中恢复中间捕获(据我所知),但是我的一些可怕的python应该可以完成这项工作。

我还制作了一本随机字典,因为对我来说不清楚你在问什么。

import re

var = """

sw1:FID256:root> ipaddrshow 

CHASSIS
Ethernet IP Address: 10.17.11.10
Ethernet Subnetmask: 255.255.210.0

CP0
Ethernet IP Address: 10.17.11.11
Ethernet Subnetmask: 255.255.210.0
Host Name: cp0
Gateway IP Address: 10.17.48.1

CP1
Ethernet IP Address: 10.17.11.12
Ethernet Subnetmask: 255.255.210.0
Host Name: cp1
Gateway IP Address: 10.17.18.1

sw1:FID256:root>
"""

rgx = re.compile(r'(?P<name>^[A-Z0-9]+)\n|(?P<attr>^[\w]+[^:]+):\s(?P<val>[\d\w\.]+)\n', re.MULTILINE)

dict = {
    "Ethernet IP Address": "ip",
    "Ethernet Subnetmask": "mask",
    "Gateway IP Address": "gw",
    "Host Name": ""
}

def translate(attr):
    return dict[attr]

def build_list(r):
    entry = ""
    name = ""

    for l in rgx.finditer(var):

        if(l.group("name")):
            if(entry):
                r.append(entry[:-1])
                entry = ""

            name = l.group("name").lower()

        elif(l.group("attr")):
            varname = translate(l.group("attr"))
            value = l.group("val")

            if(varname != ""):
                entry += "{0},{1} {2} ".format(name, varname, value)

    # add last entry
    r.append(entry[:-1])
    entry = ""

def build_dict(d):
    entry = ""
    name = ""

    for l in rgx.finditer(var):

        if(l.group("name")):
            name = l.group("name").lower()
            d[name] = {}
        elif(l.group("attr")):
            varname = translate(l.group("attr"))
            value = l.group("val")

            if(varname != ""):
                d[name][varname] = value

r = []

build_list(r)

print r

d = {}

build_dict(d)

print d

Demo