QuestionQ61

VerifiedDevice-Level Network Automation

An engineer needs to add basic reliability to a monitoring script that polls uptime from multiple Cisco IOS routers. Implement appropriate exception handling for timeouts and authentication failures while preserving successful output. Construct the artifact by dragging the code snippets into the code boxes. Not every option is needed, and options can be used more than once.

Drag & Drop
from netmiko import 
from netmiko.ssh_exception import , 

devices = [
    {"device_type": "cisco_ios", "ip": "203.0.113.31", "username": "netops", "password": "C1sco12345"},
    {"device_type": "cisco_ios", "ip": "203.0.113.32", "username": "netops", "password": "C1sco12345"}
]

for dev in devices:
    try:
        with (**dev) as conn:
            uptime = conn.("show version | inc uptime")
            print(f"{dev['ip']}: {uptime.strip()}")
    except (, ) as err:
        print(f"{dev['ip']}: ERROR {err}")
Explanation

ConnectHandler establishes a Netmiko session using the device dictionary, and send_command returns the output of a show command. NetmikoTimeoutException and NetmikoAuthenticationException specifically handle connection timeouts and authentication failures, allowing the loop to report a failure for one router and continue polling the others.

Community Discussion

No comments yet. Be the first to start the discussion!