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

Bug in Template cisco_xr_admin_show_inventory #1393

Open
muhammad-rafi opened this issue Jul 6, 2023 · 1 comment
Open

Bug in Template cisco_xr_admin_show_inventory #1393

muhammad-rafi opened this issue Jul 6, 2023 · 1 comment

Comments

@muhammad-rafi
Copy link

muhammad-rafi commented Jul 6, 2023

ISSUE TYPE
  • Template Issue with error and raw data
TEMPLATE USING
Value Required NAME (.*?)
Value DESCR (.*)
Value PID (\S+)
Value VID (\S+)
Value SN (\S*)

Start
  ^NAME:\s+"${NAME}",\s+DESCR:\s+"${DESCR}"\s*$$
  ^\s*Name:\s+${NAME}\s+Descr:\s+${DESCR}\s*$$
  ^\s*PID:\s+${PID}\s*,\s+VID:\s+${VID}\s*,\s+SN:\s*${SN}$$ -> Record
  ^\s*PID:\s+${PID}\s+VID:\s+${VID}\s+SN:\s+${SN}$$ -> Record
  #
  ^\s*$$ 
  ^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s\S{3}\s+\d
  ^. -> Error
SAMPLE COMMAND OUTPUT
$ python sh_ver_test.py 
Traceback (most recent call last):
  File "/test_scripts/sh_ver_test.py", line 45, in <module>
    output = net_connect.send_command('admin show inventory', use_textfsm=True)
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/netmiko/utilities.py", line 592, in wrapper_decorator
    return func(self, *args, **kwargs)
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/netmiko/base_connection.py", line 1798, in send_command
    return_val = structured_data_converter(
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/netmiko/utilities.py", line 556, in structured_data_converter
    structured_output_tfsm = get_structured_data_textfsm(
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/netmiko/utilities.py", line 379, in get_structured_data_textfsm
    output = _textfsm_parse(textfsm_obj, raw_output, attrs)
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/netmiko/utilities.py", line 343, in _textfsm_parse
    tfsm_parse(raw_output, attrs)
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/textfsm/clitable.py", line 282, in ParseCmd
    self.table = self._ParseCmdItem(self.raw, template_file=template_files[0])
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/textfsm/clitable.py", line 315, in _ParseCmdItem
    for record in fsm.ParseText(cmd_input):
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/textfsm/parser.py", line 895, in ParseText
    self._CheckLine(line)
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/textfsm/parser.py", line 944, in _CheckLine
    if self._Operations(rule, line):
  File "/Users/rafi/netbot_env/lib/python3.10/site-packages/textfsm/parser.py", line 1024, in _Operations
    raise TextFSMError('State Error raised. Rule Line: %s. Input Line: %s'
textfsm.parser.TextFSMError: State Error raised. Rule Line: 15. Input Line:  PID: R-IOSXRV9000-CC        VID: V01                   SN: 767D97E4B21 
SUMMARY

Every time I ran this command with netmiko along with ntc-template, I get above error regardless the version of iosxr, I looked into it and seen that this parser was fixed recently, even though it was improvised but yet it is broken. I can fix this via following changes in the template

Value Required NAME (.*?)
Value DESCR (.*?)
Value PID (\S+)
Value VID (\S+)
Value SN (\S*)

Start
  ^NAME:\s+"${NAME}",\s+DESCR:\s+"${DESCR}"\s*$$
  ^\s*Name:\s+${NAME}\s+Descr:\s+${DESCR}\s*$$
  ^\s*PID:\s+${PID}\s*,\s+VID:\s+${VID}\s*,\s+SN:\s*${SN} -> Record
  ^\s*PID:\s+${PID}\s+VID:\s+${VID}\s+SN:\s+${SN} -> Record
  #
  ^\s*$$
  ^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s\S{3}\s+\d
  ^. -> Error
STEPS TO REPRODUCE

You can use following simple python script to reproduce it

from ntc_templates.parse import parse_output
from pprint import pprint

data1 = """
Thu Jul  6 08:36:32.002 UTC
  
 Name: Rack 0                Descr: Cisco IOS-XRv 9000 Centralized Virtual Router               
 PID: R-IOSXRV9000-CC        VID: V01                   SN: FOC1234PA56 
 
 Name: 0/0                   Descr: Cisco IOS-XRv 9000 Centralized Line Card                    
 PID: R-IOSXRV9000-LC-C      VID: V01                   SN: FOC4321PA6D 
 
 Name: 0/RP0                 Descr: Cisco IOS-XRv 9000 Centralized Route Processor              
 PID: R-IOSXRV9000-RP-C      VID: V01                   SN: FOC7085NP2K 
"""

data2 = """
Thu Jul  6 08:36:19.119 UTC
NAME: "module 0/RP0/CPU0", DESCR: "ASR 9900 Route Processor for Service Edge"
PID: ASR-9900-RP-SE, VID: V04, SN: FOC1234PA56

NAME: "module 0/RP1/CPU0", DESCR: "ASR 9900 Route Processor for Service Edge"
PID: ASR-9900-RP-SE, VID: V04, SN: FOC4321PA6D

NAME: "fantray 0/FT0/SP", DESCR: "ASR 9912 Fan Tray"
PID: ASR-9912-FAN, VID: V02, SN: FOC7085NP2K

NAME: "fan0 0/FT0/SP", DESCR: "ASR9K Generic Fan"
PID: N/A, VID: N/A, SN: 

"""

output1 = parse_output(platform="cisco_xr", 
                      command=f"admin show inventory", 
                      data=data1)

output2 = parse_output(platform="cisco_xr", 
                      command=f"admin show inventory", 
                      data=data2)


print('*'*80)
pprint(output1)
print('*'*80)
print('*'*80)
pprint(output2)
print('*'*80)
EXPECTED RESULTS
********************************************************************************
[{'descr': 'Cisco IOS-XRv 9000 Centralized Virtual Router',
  'name': 'Rack 0',
  'pid': 'R-IOSXRV9000-CC',
  'sn': 'FOC1234PA56',
  'vid': 'V01'},
 {'descr': 'Cisco IOS-XRv 9000 Centralized Line Card',
  'name': '0/0',
  'pid': 'R-IOSXRV9000-LC-C',
  'sn': 'FOC4321PA6D',
  'vid': 'V01'},
 {'descr': 'Cisco IOS-XRv 9000 Centralized Route Processor',
  'name': '0/RP0',
  'pid': 'R-IOSXRV9000-RP-C',
  'sn': 'FOC7085NP2K',
  'vid': 'V01'}]
********************************************************************************
********************************************************************************
[{'descr': 'ASR 9900 Route Processor for Service Edge',
  'name': 'module 0/RP0/CPU0',
  'pid': 'ASR-9900-RP-SE',
  'sn': 'FOC1234PA56',
  'vid': 'V04'},
 {'descr': 'ASR 9900 Route Processor for Service Edge',
  'name': 'module 0/RP1/CPU0',
  'pid': 'ASR-9900-RP-SE',
  'sn': 'FOC4321PA6D',
  'vid': 'V04'},
 {'descr': 'ASR 9912 Fan Tray',
  'name': 'fantray 0/FT0/SP',
  'pid': 'ASR-9912-FAN',
  'sn': 'FOC7085NP2K',
  'vid': 'V02'},
 {'descr': 'ASR9K Generic Fan',
  'name': 'fan0 0/FT0/SP',
  'pid': 'N/A',
  'sn': '',
  'vid': 'N/A'}]
********************************************************************************
ACTUAL RESULTS

textfsm.parser.TextFSMError: State Error raised. Rule Line: 15. Input Line: PID: R-IOSXRV9000-CC VID: V01 SN: FOC1234PA56


@muhammad-rafi
Copy link
Author

#1396 raised the pull request

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

1 participant