我试图从思科路由器的多个接口的输出中解析信息。我的目标是一个字典,在那里我可以使用命名的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'}}}
我不知道如何在Python PyPi regex模块中解决您的问题,但下面是使用re-module的解决方案。我想这可能对你有帮助。在
输出:
^{pr2}$为了更好地理解代码,请删除上面的注释。如果您对代码有任何疑问,请在下面留言。在
相关问题 更多 >
编程相关推荐