使用regex/python解析Cisco接口

2024-09-25 08:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从思科路由器的多个接口的输出中解析信息。我的目标是一个字典,在那里我可以使用命名的regex捕获来提取所需的信息。在

我尝试过使用Python PyPi regex module使用预定义的子模式,但在每次匹配中获取多个命名捕获时遇到问题。理想情况下,我希望将所有项捕获到每个接口的单个匹配项:

Match 1:
Interface: x/x/x/1
MTU: 1234
BIA: x.x.x.1

Match 2:
Interface: x/x/x/2
MTU: 4321
BIA: x.x.x.2

等等,对于我输出中的每个接口。在

这是到目前为止我的正则表达式,在我尝试捕获任何额外的输出之前,它工作得很好。一旦我添加正则表达式来匹配其他项(即MTU),我就再也找不到匹配项了。我错过了什么?在

^{pr2}$

测试字符串:

    TenGigE0/1/0/0 is up, line protocol is up
      Interface state transitions: 7
      Hardware is TenGigE, address is 0026.982f.6fc0 (bia 0026.982f.6fc0)
      Layer 1 Transport Mode is LAN
      Description: LAB1
      Internet address is 172.25.26.1/31
      MTU 9172 bytes, BW 10000000 Kbit (Max: 10000000 Kbit)
         reliability 255/255, txload 62/255, rxload 192/255

    HundredGigE0/1/0/1 is up, line protocol is up
      Interface state transitions: 1
      Hardware is TenGigE, address is 0026.982f.6fc1 (bia 0026.982f.6fc1)
      Layer 1 Transport Mode is LAN
      Description: LAB2
      Internet address is 172.25.25.1/31
      MTU 9192 bytes, BW 10000000 Kbit (Max: 10000000 Kbit)
         reliability 255/255, txload 150/255, rxload 20/255

目标:

    {'interfaces': {'HundredGigE0/1/0/1': {'IP Address': '172.25.25.1/31',
                                           'MTU': '9192',
                                           'bia': '0026.982f.6fc1'},
                    'TenGigE0/1/0/0': {'IP Address': '172.25.26.1/31',
                                       'MTU': '9172',
                                       'bia': '0026.982f.6fc0'}}}

Tags: 信息目标isaddressmatch命名interfaceregex
1条回答
网友
1楼 · 发布于 2024-09-25 08:26:08

我不知道如何在Python PyPi regex模块中解决您的问题,但下面是使用re-module的解决方案。我想这可能对你有帮助。在

import re
testString="""    TenGigE0/1/0/0 is up, line protocol is up
      Interface state transitions: 7
      Hardware is TenGigE, address is 0026.982f.6fc0 (bia 0026.982f.6fc0)
      Layer 1 Transport Mode is LAN
      Description: LAB1
      Internet address is 172.25.26.1/31
      MTU 9172 bytes, BW 10000000 Kbit (Max: 10000000 Kbit)
         reliability 255/255, txload 62/255, rxload 192/255

    HundredGigE0/1/0/1 is up, line protocol is up
      Interface state transitions: 1
      Hardware is TenGigE, address is 0026.982f.6fc1 (bia 0026.982f.6fc1)
      Layer 1 Transport Mode is LAN
      Description: LAB2
      Internet address is 172.25.25.1/31
      MTU 9192 bytes, BW 10000000 Kbit (Max: 10000000 Kbit)
         reliability 255/255, txload 150/255, rxload 20/255"""

#print(testString)
#matchobj=re.search(r"(?P<intrfname>[A-Za-z]+[01]/[01]/[01]/[01]).*?\(bia (?P<bia>[a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4})\).*?Internet address is (?P<ipaddr>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d\d{1,5}).*?MTU (?P<mtu>[0-9]+ )",testString,re.M|re.DOTALL)
#if matchobj:
        #print( "\n\nHERE IT IS :: \nInterface : " + matchobj.group('intrfname')  + "\n\nBIA : " + matchobj.group('bia')+ "\n\nIP Address : " +  matchobj.group('ipaddr') + "\n\nMTU : " + matchobj.group('mtu') + "\n\n"+ matchobj.group())

interfaces=re.findall(r"(?P<intrfname>[A-Za-z]+[01]/[01]/[01]/[01]).*?\(bia (?P<bia>[a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4})\).*?Internet address is (?P<ipaddr>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d\d{1,5}).*?MTU (?P<mtu>[0-9]+ )",testString,re.M|re.DOTALL)
#print(interfaces)
result={"interfaces":{}}
#print(result)
for interface in interfaces:
        interfaceName=interface[0]
        result["interfaces"][interfaceName]={}
        result["interfaces"][interfaceName]["IP Address"]=interface[2]
        result["interfaces"][interfaceName]["MTU"]=interface[3]
        result["interfaces"][interfaceName]["bia"]=interface[1]

print(result)

输出:

^{pr2}$

为了更好地理解代码,请删除上面的注释。如果您对代码有任何疑问,请在下面留言。在

相关问题 更多 >