ktbyers / netmiko

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

netmiko error trying to install Cisco ios xe firmware #3470

Open bkyarnell opened 3 months ago

bkyarnell commented 3 months ago

Description of Issue/Question

I haven't been able to clear this error and looking for advise. The line the traceback is coming from :

net_scp_connect.send_command('y', 
       strip_prompt=False,
       strip_command=False,
       read_timeout=10
)

I've tried adding an expect state to the above, expect_string=r'#', but it also throws the same traceback. The code runs with the firmware being installed, but trying to clean this up. I'm sure it's an issue with me trying to scrape the output correctly. Any advise?

From the session log, netmiko-log.txt:

  Host1#install add file flash:csr1000v-universalk9.17.03.08a.SPA.bin activate commit
  install_add_activate_commit: START Sat Aug  3 14:32:00 UTC 2024
  install_add_activate_commit: Adding PACKAGE
  install_add_activate_commit: Checking whether new add is allowed ....

  --- Starting Add ---
  Performing Add on Active/Standby
    [1] Add package(s) on R0
    [1] Finished Add on R0
  Checking status of Add on [R0]
  Add: Passed on [R0]
  Finished Add

  Image added. Version: 17.03.08a.0.6
  install_add_activate_commit: Activating PACKAGE
  Following packages shall be activated:
  /bootflash/csr1000v-rpboot.17.03.08a.SPA.pkg
  /bootflash/csr1000v-mono-universalk9.17.03.08a.SPA.pkg

  This operation may require a reload of the system. Do you want to proceed? [y/n]
   [y/n]^J
   [y/n]y^J
  --- Starting Activate ---
  Performing Activate on Active/Standby

  exit

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

Setup

Netmiko version

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

netmiko==4.3.0

Netmiko device_type (if relevant to the issue)

(Paste device_type between quotes below)

host = {'device_type':device_type, 
                        'host': switch,
                        'username': Username,
                        'password': Password,
                        'secret': NetworkSecret,
                        'session_log': 'netmiko-log.txt'
                        }

where device_type = 'cisco_xe'.  I've also enabled the session log to write to netmiko-log.txt.

Steps to Reproduce the Issue

Error Traceback

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

Traceback (most recent call last):
  File "/home/byarnell/scripts/Python/IS1_Firmware_Loader/Functions/IS1_Firmware_Loader_Functions.py", line 342, in install_firmware
    net_scp_connect.send_command('y',
  File "/home/byarnell/.local/lib/python3.12/site-packages/netmiko/base_connection.py", line 110, in wrapper_decorator
    return_val = func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/byarnell/.local/lib/python3.12/site-packages/netmiko/utilities.py", line 595, in wrapper_decorator
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/byarnell/.local/lib/python3.12/site-packages/netmiko/base_connection.py", line 1824, in send_command
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout:
Pattern not detected: '\\[y/n\\]' in output.

Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.

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

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)

cmd1 = 'install add file flash:' + firmware_file_name + ' activate commit'
net_scp_connect.send_command(cmd1, 
       expect_string=r'This operation may require a reload of the system', 
       strip_prompt=False,
       strip_command=False,
       read_timeout=500
)
time.sleep(5)
net_scp_connect.send_command('y', 
       strip_prompt=False,
       strip_command=False,
       read_timeout=10
 )
ktbyers commented 3 months ago

It looks like the pattern and your code don't match?

Pattern not detected: '\\[y/n\\]' in output.

Here it looks like you are looking for a [y/n] pattern?

bkyarnell commented 3 months ago

Yes, it does seem that way. These are the lines I'm trying to catch and throw a reply:

install add file flash:csr1000v-universalk9.17.03.07.SPA.bin activate commit
This operation may require a reload of the system. Do you want to proceed? [y/n]y

I was hoping expect_string=r'This operation may require a reload of the system' would capture? I've tried a lot of variation with that without luck.

ktbyers commented 3 months ago

Can you post the full exception stack that you receive (when you use this code that you posted):

cmd1 = 'install add file flash:' + firmware_file_name + ' activate commit'
net_scp_connect.send_command(cmd1, 
       expect_string=r'This operation may require a reload of the system', 
       strip_prompt=False,
       strip_command=False,
       read_timeout=500
)
time.sleep(5)
net_scp_connect.send_command('y', 
       strip_prompt=False,
       strip_command=False,
       read_timeout=10
 )

The exception stack you posted above does not look like it matches this code (based on the output from the exception).

bkyarnell commented 3 months ago

Agree, that's odd! But, the code and stack trace is accurate. It doesn't appear to be looking for the expect_string I'm using in the code. expect_string=r'This operation may require a reload of the system'. It throws the exception, but the send command 'y' seems to be allowing it to progress.

ktbyers commented 3 months ago

Add an expect_string onto this second call as well:

net_scp_connect.send_command('y', 
       strip_prompt=False,
       strip_command=False,
       read_timeout=10
 )

Like expect_string=r"#" (or whatever is in the trailing prompt)

bkyarnell commented 3 months ago

I tried some variation in the code with try/except/finally, but didn't really change anything. Line 398 in the Exception is in the Finally statements. I've also increased the read_timeout for the 'y' reply to 100 - no change. Maybe higher? End result, code completes and the update occurs. But, the Exception is annonying!

cmd1 = 'install add file flash:' + firmware_file_name + ' activate commit'
try:
      net_scp_connect.send_command(cmd1,
             expect_string=r"This operation may require a reload of the system", 
             strip_prompt=False,
             strip_command=False,
             read_timeout=500
      )
except Exception:
      time.sleep(5)
      net_scp_connect.send_command('y',
             expect_string=r"#",                         
             strip_prompt=False,
             strip_command=False,
             read_timeout=10
       )
finally:
       time.sleep(5)
       net_scp_connect.send_command('y', 
              expect_string=r"#",
              strip_prompt=False,
              strip_command=False,
              read_timeout=10
       )

Exception:

`...installing csr1000v-universalk9.17.03.08a.SPA.bin on switch 10.2.0.251
Traceback (most recent call last):
  File "/home/byarnell/scripts/Python/IS1_Firmware_Loader/Functions/IS1_Firmware_Loader_Functions.py", line 398, in install_firmware
    net_scp_connect.send_command('y',
  File "/home/byarnell/.local/lib/python3.12/site-packages/netmiko/base_connection.py", line 110, in wrapper_decorator
    return_val = func(self, *args, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/byarnell/.local/lib/python3.12/site-packages/netmiko/utilities.py", line 595, in wrapper_decorator
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/byarnell/.local/lib/python3.12/site-packages/netmiko/base_connection.py", line 1824, in send_command
    raise ReadTimeout(msg)
netmiko.exceptions.ReadTimeout:
Pattern not detected: '#' in output.

Things you might try to fix this:
1. Explicitly set your pattern using the expect_string argument.
2. Increase the read_timeout to a larger value.`

Session log:

Host1#install add file flash:csr1000v-universalk9.17.03.08a.SPA.bin activate commit
install_add_activate_commit: START Tue Aug  6 02:51:59 UTC 2024
install_add_activate_commit: Adding PACKAGE
install_add_activate_commit: Checking whether new add is allowed ....

--- Starting Add ---
Performing Add on Active/Standby
  [1] Add package(s) on R0
  [1] Finished Add on R0
Checking status of Add on [R0]
Add: Passed on [R0]
Finished Add

Image added. Version: 17.03.08a.0.6
install_add_activate_commit: Activating PACKAGE
Following packages shall be activated:
/bootflash/csr1000v-rpboot.17.03.08a.SPA.pkg
/bootflash/csr1000v-mono-universalk9.17.03.08a.SPA.pkg

This operation may require a reload of the system. Do you want to proceed? [y/n]
 [y/n]y^J
--- Starting Activate ---
Performing Activate on Active/Standby

exit