使用TextFSM在trun上查找允许的vlan

2024-05-29 11:08:40 发布

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

我正在尝试为NTC ansible设置一个TextFSM模板,它只会从“show interface trunk”命令的输出中提取主干上允许的VLAN,但似乎无法得到我想要的。它给了我所有的行而不是我想要的一行。命令的输出如下所示:

switch#sh int g9/17 trunk

Port                Mode         Encapsulation  Status        Native vlan
Gi9/17              on           802.1q         trunking      1

Port                Vlans allowed on trunk
Gi9/17              501,503,513,540,950-957

Port                Vlans allowed and active in management domain
Gi9/17              501,503,513,540,950-957

Port                Vlans in spanning tree forwarding state and not pruned
Gi9/17              501,503,513,540,950-957

在这个输出中,我只想返回“vlansallowedontrunk”下面的行,而不是其他具有相同信息的重复行。我的模板如下所示:

^{pr2}$

Tags: andin命令模板onportshowansible
1条回答
网友
1楼 · 发布于 2024-05-29 11:08:40

使正则表达式更具体,并获得所需的结果(可能:)。在

import io
import textfsm

template = io.StringIO("""\
Value Port (\S+(/\d+)?)
Value Vlans (\d+([-,]\d+)+)

Start
  ^Port\s+Vlans allowed on trunk$$ -> Begin

Begin
  ^${Port}\s+${Vlans}$$ -> Record
  ^Port\s+Vlans allowed and active in management domain$$ -> End
""")
fsm = textfsm.TextFSM(template)
result = fsm.ParseText("""\
switch#sh int g9/17 trunk

Port                Mode         Encapsulation  Status        Native vlan
Gi9/17              on           802.1q         trunking      1

Port                Vlans allowed on trunk
Gi9/17              501,503,513,540,950-957

Port                Vlans allowed and active in management domain
Gi9/17              501,503,513,540,950-957

Port                Vlans in spanning tree forwarding state and not pruned
Gi9/17              501,503,513,540,950-957
""")
print(result)

^{pr2}$

相关问题 更多 >

    热门问题