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 IOS - textfsm.parser.TextFSMError: State Error raised. Rule Line: 17. #718

Closed
rapowell opened this issue May 21, 2020 · 7 comments · Fixed by #725
Closed

Cisco IOS - textfsm.parser.TextFSMError: State Error raised. Rule Line: 17. #718

rapowell opened this issue May 21, 2020 · 7 comments · Fixed by #725

Comments

@rapowell
Copy link

ISSUE TYPE
  • Bug Report
TEMPLATE USING

cisco_ios_show_interfaces_description.textfsm

Value PORT (\S+)
Value STATUS (up|down|admin\s+down)
Value PROTOCOL (up|down)
Value DESCRIP (\S.*?)

Start
  ^Interface\s+Status\s+Protocol\s+Description\s*$$ -> Begin
  ^\s*$$
  # Capture time-stamp if vty line has command time-stamping turned on
  ^Load\s+for\s+
  ^Time\s+source\s+is
  ^. -> Error

Begin
  ^${PORT}\s+${STATUS}\s+${PROTOCOL}(?:\s+${DESCRIP})?\s*$$ -> Record
  ^\s*$$
  ^. -> Error

SAMPLE COMMAND OUTPUT
RTR-01#sh int desc
Interface                      Status         Protocol Description
Gi0/0/0                        up             up       Connected to S01 Port Gi1/6/2 
Gi0/0/1                        up             up       Connected to S02 Gi2/6/2 
Gi0/0/2                        admin down     down
Gi0/0/3                        up             up       Bypass Link to Gi1/6/37
Gi0/0/4                        admin down     down
Gi0/0/5                        up             up       ISP Connection
Gi0/0/5.25                     deleted        down
Gi0/0/5.60                     up             up       ISP
Se0/1/0                        admin down     down
Se0/1/1                        admin down     down
Gi0                            admin down     down
Lo0                            up             up
Lo10                           up             up       TUNNEL ENDPOINT
Tu0                            up             up
SUMMARY

When attempting to parse the output from the sh int desc (show interface description) command on Cisco IOS, I get an Error as shown below. I attempted to modify the template as follows, but that did not fix the issue

Value PORT (\S+)
Value STATUS (up|down|admin\s+down|deleted)
Value PROTOCOL (up|down)
Value DESCRIP (\S.*?)

Start
  ^Interface\s+Status\s+Protocol\s+Description\s*$$ -> Begin
  ^\s*$$
  # Capture time-stamp if vty line has command time-stamping turned on
  ^Load\s+for\s+
  ^Time\s+source\s+is
  ^. -> Error

Begin
  ^${PORT}\s+${STATUS}\s+${PROTOCOL}(?:\s+${DESCRIP})?\s*$$ -> Record
  ^\s*$$
  ^. -> Error
STEPS TO REPRODUCE
from netmiko import ConnectHandler
from getpass import getpass

RTR_IP = '10.1.1.5'
user = 'admin'
cmd = 'show int desc'
device = ConnectHandler(device_type='cisco_ios', ip=RTR_IP, username=user, password=getpass("Enter your PASSCODE: "))
device.disable_paging()
output = device.send_command(cmd, delay_factor=2, use_textfsm=True)
EXPECTED RESULTS
>>> output = device.send_command(cmd, delay_factor=2, use_textfsm=True)
>>> output
[{'port': 'Gi0/0/0', 'status': 'up', 'protocol': 'up', 'descrip': 'Connected to S01 Port Gi1/6/2'}, 
{'port': 'Gi0/0/1', 'status': 'up', 'protocol': 'up', 'descrip': 'Connected to S02 Gi2/6/2'}, 
{'port': 'Gi0/0/2', 'status': 'admin down', 'protocol': 'down', 'descrip': ''}, 
{'port': 'Gi0/0/3', 'status': 'up', 'protocol': 'up', 'descrip': 'Bypass Link to Gi1/6/37'}, 
{'port': 'Gi0/0/4', 'status': 'admin down', 'protocol': 'down', 'descrip': ''}, 
{'port': 'Gi0/0/5', 'status': 'up', 'protocol': 'up', 'descrip': 'ISP Connection'}, 
{'port': 'Gi0/0/5.25', 'status': 'deleted', 'protocol': 'down', 'descrip': ''}, 
{'port': 'Gi0/0/5.60', 'status': 'up', 'protocol': 'up', 'descrip': 'ISP'}, 
{'port': 'Se0/1/0', 'status': 'admin down', 'protocol': 'down', 'descrip': ''}, 
{'port': 'Se0/1/1', 'status': 'admin down', 'protocol': 'down', 'descrip': ''}, 
{'port': 'Gi0', 'status': 'admin down', 'protocol': 'down', 'descrip': ''}, 
{'port': 'Lo0', 'status': 'up', 'protocol': 'up', 'descrip': ''},
{'port': 'Lo10', 'status': 'up', 'protocol': 'up', 'descrip': 'TUNNEL ENDPOINT'},
{'port': 'Tu0', 'status': 'up', 'protocol': 'up', 'descrip': ''}]
ACTUAL RESULTS
output = device.send_command(cmd, delay_factor=2, use_textfsm=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\site-packages\netmiko\utilities.py", line 347, in wrapper_decorator
    return func(self, *args, **kwargs)
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\site-packages\netmiko\base_connection.py", line 1444, in send_command
    structured_output = get_structured_data(
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\site-packages\netmiko\utilities.py", line 280, in get_structured_data
    return _textfsm_parse(textfsm_obj, raw_output, attrs)
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\site-packages\netmiko\utilities.py", line 252, in _textfsm_parse
    textfsm_obj.ParseCmd(raw_output, attrs)
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\site-packages\netmiko\_textfsm\_clitable.py", line 272, in ParseCmd
    self.table = self._ParseCmdItem(self.raw, template_file=template_files[0])
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\site-packages\netmiko\_textfsm\_clitable.py", line 303, in _ParseCmdItem
    for record in fsm.ParseText(cmd_input):
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\site-packages\textfsm\parser.py", line 895, in ParseText
    self._CheckLine(line)
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\site-packages\textfsm\parser.py", line 944, in _CheckLine
    if self._Operations(rule, line):
  File "C:\Users\RP\AppData\Local\Programs\Python\Python38\lib\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: 17. Input Line: Gi0/0/5.61                     deleted        down
@itdependsnetworks
Copy link
Contributor

Can likely change Value STATUS (up|down|admin\s+down) -> Value STATUS (up|down|admin\s+down|deleted) and put in a PR for the same.

@rapowell
Copy link
Author

I tried that and I still get the error

@FragmentedPacket
Copy link
Contributor

FragmentedPacket commented May 21, 2020

@rapowell What you have in your post and what @itdependsnetworks stated works. I would look into how Netmiko is pulling those templates and where it is looking for those and make sure you made the change in the correct location.

I can submit a PR to push the fix up, but if you haven't had the experience of doing so, we typically recommend submitting the PR yourself.

@rapowell
Copy link
Author

@FragmentedPacket and @itdependsnetworks - Thanks for the feedback and validation that what I thought should work should work. On a related not, I had also tried to modify the index file because the "show cdp neighbors detail" command wasn't parsing the output when I entered it as "sh cdp nei det" and even after changing it in the index file it didn't seem to pick up the change. I'll attempt to submit the PR for textfsm and I'll open up an issue with netmiko to see if the author can point me in the right direction. Thanks again.

@ktbyers
Copy link
Contributor

ktbyers commented May 21, 2020

Should be detailed here:

https://pynet.twb-tech.com/blog/automation/netmiko-textfsm.html

But basically you need to set the NET_TEXTFSM environment variable (and point to directory containing the TextFSM index file).

There is PR to use the PIP-package, but I haven't got the review of that done yet.

@rapowell
Copy link
Author

@ktbyers Thanks for the update. Now I need to work out how to make that happen on a Windows machine that I don't have admin rights to.

@rapowell
Copy link
Author

I think I figured it out in case anyone else encounters this problem, this is what I did.

import os

PIP_TEXTFSM = r'C:\Users\RP\AppData\Local\Programs\Python\Python38\Lib\site-packages\ntc_templates\templates'
os.environ["NET_TEXTFSM"] = PIP_TEXTFSM

Now I need to verify this will work inside a script and I will have a good workaround. Thanks.

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