A network engineer is writing a Python script to automate the backup of running configurations from a list of 50 Cisco IOS-XE devices. The script uses the netmiko library and a for loop to connect to each device, execute 'show run', and write the output to a file. After running the script, the engineer notices that the script fails on the 15th device with a timeout error, and the remaining devices are not processed. The engineer wants to ensure that if one device fails, the script continues with the next device. What is the best way to modify the script?
Trap 1: Increase the global timeout value in the netmiko connection handler.
Incorrect because increasing the timeout may delay the failure but will not prevent the script from stopping when an exception occurs.
Trap 2: Use the concurrent.futures module to run each connection in a…
Incorrect because multithreading does not automatically handle exceptions; a failure in one thread would still need to be caught, and the script may still stop if not handled.
Trap 3: Replace the for loop with a while loop that retries the connection…
Incorrect because a while loop with retries does not prevent the script from stopping if an unhandled exception occurs; it only adds retry logic.
- A
Increase the global timeout value in the netmiko connection handler.
Why wrong: Incorrect because increasing the timeout may delay the failure but will not prevent the script from stopping when an exception occurs.
- B
Use the concurrent.futures module to run each connection in a separate thread.
Why wrong: Incorrect because multithreading does not automatically handle exceptions; a failure in one thread would still need to be caught, and the script may still stop if not handled.
- C
Wrap the connection and backup logic inside a try-except block within the for loop.
Correct because a try-except block catches the exception for the failing device and allows the loop to continue to the next device.
- D
Replace the for loop with a while loop that retries the connection three times before moving on.
Why wrong: Incorrect because a while loop with retries does not prevent the script from stopping if an unhandled exception occurs; it only adds retry logic.