ktbyers / netmiko

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

Seeing Pattern not detected error due to Hostname change after reloading a switch using netmiko #3523

Closed pnikila closed 1 week ago

pnikila commented 1 week ago

Description of Issue/Question

I am resetting a switch using serial(console) connection with write erase and reload. And Checking the prompt to see if reload is completed. Once reload is completed, we need to add additional configs like username and password and other basic commands.

Setup

device_dict_cisco_serial = { "device_type" : "terminal_server", "ip":device_dict_cisco["serial_ip"], "port":device_dict_cisco["serial_port"], "username":device_dict_cisco["serial_username"], "password":device_dict_cisco["serial_password"], "global_delay_factor": 3, "session_log":device_dict_cisco["session_log"], "secret":device_dict_cisco["secret"] } netmiko_dev = ConnectHandler(**device_dict_cisco_serial) netmiko.redispatch(netmiko_dev, device_type=device_dict_cisco["device_type"])

Netmiko version

(Paste verbatim output from pip freeze | grep netmiko between quotes below)

netmiko            4.4.0

Netmiko device_type (if relevant to the issue)

(Paste device_type between quotes below)

cisco_ios

Steps to Reproduce the Issue

1)Connect to serial console 2) Give "Write erase" 3) Give reload 4) There is change in hostname after reload command is issued .

Error Traceback

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

Retry 20:Waiting for router reload. Time taken since reload: 00:05:27

sw-10-64-225-143>
PnP HTTP timed out (2/10) on connection to PnP server (https://devicehelper.cisco.com.:443/pnp/HELLO)

*Nov  4 12:39:34.489: %PNP-3-PNP_HTTP_TIMED_OUT: PnP HTTP timed out (2/10) on connection to PnP server (https://devicehelper.cisco.com.:443/pnp/HE
LLO)

Retry 21: Exception occured:

Pattern not detected: 'sw\\-10\\-64\\-224\\-66.*' 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.

Traceback (most recent call last):

  File "D:\Users\t0-905498\Documents\device_config_mgmt_v3\device_config_mgmt\device_run.py", line 151, in reload_device
    netmiko_device.enable()
  File "....\Lib\site-packages\netmiko\cisco_base_connection.py", line 27, in enable
    return super().enable(
           ^^^^^^^^^^^^^^^
  File "...\Lib\site-packages\netmiko\base_connection.py", line 2026, in enable
    if check_state and self.check_enable_mode():
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Lib\site-packages\netmiko\cisco_base_connection.py", line 16, in check_enable
_mode
    return super().check_enable_mode(check_string=check_string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Lib\site-packages\netmiko\base_connection.py", line 1995, in check_enable_mod
e
    output = self.read_until_prompt(read_entire_line=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Lib\site-packages\netmiko\base_connection.py", line 839, in read_until_prompt

    return self.read_until_pattern(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\Lib\site-packages\netmiko\base_connection.py", line 738, in read_until_patter
n
    time.sleep(loop_delay)
KeyboardInterrupt

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)

print("\n\n### Device Reload ###")
    # # Confirm reload (if prompted)
    output = netmiko_device.send_command_timing("reload", strip_prompt=False, strip_command=False)

    prompt = netmiko_device.find_prompt()    

    reload_start_time, reload_start_time_seconds = get_time()
    print("\nReload started at: ",reload_start_time)
    netmiko_device.write_channel("\r")
    attempt = 0
    time.sleep(30)

    while attempt<=20:
        try:
            netmiko_device.write_channel("\r")
            prompt = netmiko_device.read_channel()
            print("\n" + prompt)
            attempt= attempt + 1
            if '>' in prompt:
                netmiko_device.enable()
                print("\nDevice Reload Success")
                reload_end_time, reload_end_time_seconds = get_time()
                print("\nReload completed at: ",reload_end_time)

                reload_duration=time.strftime("%H:%M:%S",time.gmtime(reload_end_time_seconds-reload_start_time_seconds))                                                     
                print("\nDevice Reload Duration:", reload_duration)
                print(f"\n\n### Device '{hostnamedev}'reload success at {reload_end_time} ###")
                break
            else:
                curr_time, curr_time_sec = get_time()
                time_taken=time.strftime("%H:%M:%S",time.gmtime(curr_time_sec-reload_start_time_seconds))
                print(f"\nRetry {attempt}:Waiting for router reload. Time taken since reload: {time_taken}")
                time.sleep(5)
        except Exception as e:
            print(f"\nRetry {attempt}: Exception occured: {str(e)}")
ktbyers commented 1 week ago

I would probably try to call set_base_prompt() before you make your .enable() call, here:

            if '>' in prompt:
                netmiko_device.enable()

Let me know if that fixes the issue.

pnikila commented 1 week ago

Thanks @ktbyers. It is working now.