kirkofthefleet
kirkofthefleet

Reputation: 21

Netmiko Recv'd Strange Characters Breaking Find Prompt Function

I am writing a python script to pull some information on some routers. I am having an issue where for some reason netmiko is reading some BS characters (quite literally) instead of the end of the string it is looking for.

Here is the traceback. Please note that the first line below is output from the script telling me what command it is currently running:

Running sh l2vpn forwarding bridge-domain mac-address location 0/0/CPU0 |  i .501 | utility wc -l
Traceback (most recent call last):
  File "/mnt/scriptdev/gitlab/hotwiregit/cgnat_uplink_finder/joseStyle/./js_cgnatuf.py", line 86, in <module>
    maccount = _cisco_connect(rtrLoop,command)
  File "/mnt/scriptdev/gitlab/hotwiregit/cgnat_uplink_finder/joseStyle/functions/hwcconnect_cisco.py", line 24, in _cisco_connect
    cisco_output = net_connect.send_command(command,expect_string=r'#',read_timeout=300)
  File "/home/tymier/.local/lib/python3.10/site-packages/netmiko/base_connection.py", line 110, in wrapper_decorator
    return_val = func(self, *args, **kwargs)
  File "/home/tymier/.local/lib/python3.10/site-packages/netmiko/utilities.py", line 595, in wrapper_decorator
    return func(self, *args, **kwargs)
  File "/home/tymier/.local/lib/python3.10/site-packages/netmiko/base_connection.py", line 1774, in send_command
    new_data = self.command_echo_read(cmd=cmd, read_timeout=10)
  File "/home/tymier/.local/lib/python3.10/site-packages/netmiko/base_connection.py", line 1484, in command_echo_read
    new_data = self.read_until_pattern(
  File "/home/tymier/.local/lib/python3.10/site-packages/netmiko/base_connection.py", line 746, in read_until_pattern
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout:

Pattern not detected: 'sh\\ l2vpn\\ forwarding\\ bridge\\-domain\\ mac\\-address\\ location\\ 0/0/CPU0\\ \\|\\ \\ i\\ \\.501\\ \\|\\ utility\\ wc\\ \\-l' in output.

Things you might try to fix this:
1. Adjust the regex pattern to better identify the terminating string. Note, in
many situations the pattern is automatically based on the network device's prompt.
2. Increase the read_timeout to a larger value.

You can also look at the Netmiko session_log or debug log for more information.

Here is the relavant information from the session log:

RP/0/RSP0/CPU0:<REDACTED_HOSTNAME>#
DEBUG:netmiko:Clear buffer detects data in the channel
DEBUG:netmiko:[find_prompt()]: prompt is RP/0/RSP0/CPU0:<REDACTED_HOSTNAME>#
INFO:netmiko.ssh_dispatcher:Netmiko connection succesful to <REDACTED_IP>:22
b'sh l2vpn forwarding bridge-domain mac-address location 0/0/CPU0 |  i .501 | utility wc -l\n'
sh l2vpn forwarding bridge-domain$omain                           mac-address location 0/0/C$0/0/CP                          U0 |  i .501 | utility wc $y wc -                          lsh l2vpn forwarding bridge-domai$
Fri Mar 22 10:47:07.056 EDT
674
RP/0/RSP0/CPU0:<REDACTED_HOSTNAME>#

From what I can tell, netmiko is looking for confirmation that the command was sent to the device by checking the output for the inputted command (hoping that makes sense). There is something happening to the end of the output where all of those characters are showing up. When I load this file in notepad++ it shows [BS]. Submitting screenshot of this as when pasting, the character changes:

enter image description here

I tried increasing the read_timout to 300 seconds to give the command plenty of time in case any commands take longer than expected to run. Checking the traceback output above I see:

new_data = self.command_echo_read(cmd=cmd, read_timeout=10) File "/home/tymier/.local/lib/python3.10/site-packages/netmiko/base_connection.py", line 1484, in command_echo_read

This leads me to believe that netmiko expects to see its command input in STDOUT within 10 seconds. I think this is what is breaking the script (literally realizing this as I'm typing).

Is there a way to adjust this timeout without editing the base_connection.py file? I would like to do it in a similar fashion to using read_timeout

Upvotes: 0

Views: 61

Answers (0)

Related Questions