Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cisco_nxos_show_interfaces_switchport adds additional space to monitor statement #1317

Open
Mattias3891 opened this issue Mar 10, 2023 · 3 comments

Comments

@Mattias3891
Copy link

Mattias3891 commented Mar 10, 2023

ISSUE TYPE
  • Template Issue with error and raw data
TEMPLATE USING

latest "cisco_nxos_show_interfaces_switchport "

SAMPLE COMMAND OUTPUT
nxos-sw1# show interface Eth1/36 switchport 
Name: Ethernet1/36
  Switchport: Enabled
  Switchport Monitor: Enabled 
  Switchport Block Multicast: Not enabled 
  Switchport Block Unicast: Not enabled 
  Operational Mode: access
  Access Mode VLAN: 1 (default)
  Trunking Native Mode VLAN: 1 (default)
  Trunking VLANs Allowed: 1-4094
  Administrative private-vlan primary host-association: none
  Administrative private-vlan secondary host-association: none
  Administrative private-vlan primary mapping: none
  Administrative private-vlan secondary mapping: none
  Administrative private-vlan trunk native VLAN: none
  Administrative private-vlan trunk encapsulation: dot1q
  Administrative private-vlan trunk normal VLANs: none
  Administrative private-vlan trunk private VLANs: none
  Operational private-vlan: none
nxos-sw1# show interface Po24 switchport 
Name: port-channel24
  Switchport: Enabled
  Switchport Monitor: Not enabled 
  Switchport Block Multicast: Not enabled 
  Switchport Block Unicast: Not enabled 
  Operational Mode: trunk
  Access Mode VLAN: 1 (default)
  Trunking Native Mode VLAN: 1 (default)
  Trunking VLANs Allowed: 120,134,501,800,804
  Administrative private-vlan primary host-association: none
  Administrative private-vlan secondary host-association: none
  Administrative private-vlan primary mapping: none
  Administrative private-vlan secondary mapping: none
  Administrative private-vlan trunk native VLAN: none
  Administrative private-vlan trunk encapsulation: dot1q
  Administrative private-vlan trunk normal VLANs: none
  Administrative private-vlan trunk private VLANs: none
  Operational private-vlan: none
nxos-sw1# 

Iterating over the list of interfaces in python we get:

{'interface': 'Ethernet1/36', 'switchport': 'Enabled', 'switchport_monitor': 'Enabled ', 'mode': 'access', 'access_vlan': '1', 'access_vlan_name': 'default', 'native_vlan': '1', 'native_vlan_name': 'default', 'trunking_vlans': '1-4094', 'voice_vlan': ''}
{'interface': 'port-channel24', 'switchport': 'Enabled', 'switchport_monitor': 'Not enabled ', 'mode': 'trunk', 'access_vlan': '1', 'access_vlan_name': 'default', 'native_vlan': '1', 'native_vlan_name': 'default', 'trunking_vlans': '10-20', 'voice_vlan': ''}
SUMMARY

Of unknown reason, we get an additional space in the "switchport_monitor" string.
Thus, having to match "Enabled " and not just Enabled for example.

FIX?

To fix this, I've corrected the template to only allow Enabled/Disabled on switchport statement and Enabled/Not enabled on the switchport_monitor statement.

Value Required INTERFACE (\S+)
Value SWITCHPORT (Enabled|Disabled)
Value SWITCHPORT_MONITOR (Enabled|Not\senabled)
Value MODE (access|trunk)

Works well.

@mjbear
Copy link
Contributor

mjbear commented Jan 7, 2024

Hello @Mattias3891

I'm a community member volunteering suggestions and possibly to submit a Pull Request (if needed and if you would like).

It's pretty wild how Cisco is adding trailing spaces all of a sudden -- to quite a few things in your example output.

What version of NXOS are you seeing this with?

Conditional Logic Suggestion
I'm not saying this issue shouldn't be fixed (not my call), but this could be solved in your 🐍 Python code by checking for your desired string in that returned string as opposed to modifying the template.

# use the TextFSM structured data instead of this example dictionary
parsed = {'switchport_monitor': 'this string should contain Enabled <space>'}

if 'Enabled' in parsed['switchport_monitor']:
    print('contains Enabled, hooray!')

Reasoning
If there's anything I've learned from working with regexes and talking to the NTC team, slightly "looser" regexes mean the templates should parse when vendors and situations tweak the output in minor ways.

If SWITCHPORT and MODE were changed to have specific string values, then if/when there was another value it would not be matched, breaking the template.
https://github.com/networktocode/ntc-templates/blob/master/ntc_templates/templates/cisco_nxos_show_interfaces_switchport.textfsm

@mjbear
Copy link
Contributor

mjbear commented Feb 27, 2024

@Mattias3891
Making the regexes for the capture group more rigid is a double edged sword based on experience.

Currently:
Value SWITCHPORT_MONITOR (.+)

My suggestion:
Value SWITCHPORT_MONITOR (\S+(?:\s+\S+)?)

I'm working up a fix with my suggestion as it meets your needs and should be flexible enough that it will be less likely to break in the future.

---
parsed_sample:
  - access_vlan: "1" 
    access_vlan_name: "default"
    interface: "Ethernet1/36"
    mode: "access"
    native_vlan: "1" 
    native_vlan_name: "default"
    switchport: "Enabled"
    switchport_monitor: "Enabled"
    trunking_vlans: "1-4094"
    voice_vlan: ""
  - access_vlan: "1" 
    access_vlan_name: "default"
    interface: "port-channel24"
    mode: "trunk"
    native_vlan: "1" 
    native_vlan_name: "default"
    switchport: "Enabled"
    switchport_monitor: "Not enabled"
    trunking_vlans: "120,134,501,800,804"
    voice_vlan: ""

@mjbear
Copy link
Contributor

mjbear commented Feb 27, 2024

PR #1622 has been submitted for consideration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants