ktbyers / netmiko

Multi-vendor library to simplify Paramiko SSH connections to network devices
MIT License
3.49k stars 1.26k forks source link

Unable to parse data using a textfsm template with send_multiline() #3413

Closed seydachan closed 2 months ago

seydachan commented 2 months ago

Description of Issue/Question

I'm unable to parse out data using a textfsm template, while using the send_multiline() method. My variable is a list of commands, which works with send_multiline() if I do not include the textfsm template. It looks like send_multiline() does not work with textfsm.

I also checked with another engineer and they were able to tell me the following: Netmiko does the parsing with TextFSM it will create a List and there is an assertion in the Netmiko code which explicitly expects a string.

Note: Please check https://guides.github.com/features/mastering-markdown/ to see how to properly format your request.

Netmiko version


netmiko==4.3.0

Netmiko device_type (if relevant to the issue)

(Paste device_type between quotes below)

cisco_ios

Steps to Reproduce the Issue

Error Traceback

(Paste the complete traceback of the exception between quotes below)

Traceback (most recent call last):
  File "/c/Python/arp/./arptest1-v3.py", line 30, in <module>
    output = dist_rtr.send_multiline(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/netmiko/base_connection.py", line 1896, in send_multiline
    output += self._send_command_str(
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/netmiko/base_connection.py", line 1846, in _send_command_str
    output = self.send_command(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/netmiko/base_connection.py", line 110, in wrapper_decorator
    return_val = func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/netmiko/utilities.py", line 595, in wrapper_decorator
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/netmiko/base_connection.py", line 1832, in send_command
    return_val = structured_data_converter(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/netmiko/utilities.py", line 559, in structured_data_converter
    structured_output_tfsm = get_structured_data_textfsm(
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/netmiko/utilities.py", line 379, in get_structured_data_textfsm
    output = _textfsm_parse(textfsm_obj, raw_output, attrs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/netmiko/utilities.py", line 343, in _textfsm_parse
    tfsm_parse(raw_output, attrs)
  File "/c/Python/arp/lib/python3.12/site-packages/textfsm/clitable.py", line 282, in ParseCmd
    self.table = self._ParseCmdItem(self.raw, template_file=template_files[0])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/textfsm/clitable.py", line 315, in _ParseCmdItem
    for record in fsm.ParseText(cmd_input):
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/site-packages/textfsm/parser.py", line 895, in ParseText
    self._CheckLine(line)
  File "/c/Python/arp/lib/python3.12/site-packages/textfsm/parser.py", line 944, in _CheckLine
    if self._Operations(rule, line):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/c/Python/arp/lib/python3.12/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: show ip arp vlan 100

Relevant Python code

(Please try to essentialize your Python code to the minimum code needed to reproduce the issue) (Paste the code between the quotes below)

Code that I used:

#!/usr/bin/env python3

from netmiko import ConnectHandler
import textfsm
from rich import print as rprint

# textfsm template
template_path = "/c/Python/project"
template_file = "arp-interface.textfsm"

device_type = "cisco_ios"
username = "my_name"
password = "my_password"

dist_rtr = ConnectHandler(
    device_type=device_type,
    host="some_router",
    username=username,
    password=password,
)

vlan_arp = ["show ip arp vlan 100",
            "show ip arp vlan 101",
            "show ip arp vlan 103"
]
output = dist_rtr.send_multiline(
    vlan_arp,
    use_textfsm=True,
    textfsm_template=f"{template_path}/{template_file}"
)

rprint(output)
dist_rtr.disconnect()
ktbyers commented 2 months ago

Yes, send_multiline is not intended to work with TextFSM. You should just use a for-loop and send_command.

seydachan commented 2 months ago

Thanks