【问题标题】:Use TextFSM to find allowed VLANs on trunk使用 TextFSM 在中继上查找允许的 VLAN
【发布时间】:2018-05-17 16:58:16
【问题描述】:

我正在尝试为 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

在此输出中,我只想返回“中继上允许的 Vlan”下方的行,而不是具有相同信息的其他重复行。我的模板如下所示:

Value PORT (\S+)
Value VLANS (.*)

Start
  ^Port.*Vlans allowed on trunk -> Begin

Begin
  ^(?=\s{0,9}${PORT})\s+${VLANS} -> Record
  ^Port.*Vlans allowed and active in management domain -> End

【问题讨论】:

    标签: python regex ansible text-parsing python-textfsm


    【解决方案1】:

    使正则表达式更具体并获得所需的结果(也许:)。

    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)
    

    [['Gi9/17', '501,503,513,540,950-957']]
    

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2018-05-11
      • 2020-09-28
      相关资源
      最近更新 更多