Connect to a Cisco device with Netmiko

This Python script uses the Netmiko module to establish SSH connections to a Cisco IOS device and execute CLI commands on that device.

from netmiko import Netmiko

cisco_sandbox_devices = {
    "ios": {
        "hostname": "sandbox-iosxe-latest-1.cisco.com",
        "port": 22,
        "username": "developer",
        "password": "C1sco12345",
        "device_type": "cisco_ios",
     },
}

def netmiko_connect(device_type):

    print(
        f"\n\nConnecting to {cisco_sandbox_devices[device_type]['hostname']}:{cisco_sandbox_devices[device_type]['port']}"
    )
    print("... this may take a little while.")

    connection = Netmiko(
        cisco_sandbox_devices[device_type]["hostname"],
        port=cisco_sandbox_devices[device_type]["port"],
        username=cisco_sandbox_devices[device_type]["username"],
        password=cisco_sandbox_devices[device_type]["password"],
        device_type=cisco_sandbox_devices[device_type]["device_type"],
    )

    return connection


def disconnect(connection):
    connection.disconnect()


SHOW_IP_ROUTE = "ip route"
SHOW_ARP = "arp"
SHOW_INT_DESCRIPTION = "int description"
SHOW_INT_BRIEF = "int brief"
SHOW_VERSION = "version"

IOS = "ios"
commands = {SHOW_IP_ROUTE: {IOS: "show ip route"},
            SHOW_ARP: {IOS: "show arp"},
            SHOW_INT_DESCRIPTION: {IOS: "show interfaces description"},
            SHOW_INT_BRIEF: {IOS: "show ip interface brief"},
            SHOW_VERSION: {IOS: "show version"}
            }

# CYCLE THROUGH DIFFERENT DEVICE TYPES
for device_type in [IOS]:

    connection = netmiko_connect(device_type)
    print('connection:', connection)

    print(f"\n\n----- showing running configuration for {device_type} -------------------")
    output = connection.send_command("show running-config")
    print(output)

    print(f"\n\n----- showing ip route for {device_type} -------------------")
    output = connection.send_command(commands[SHOW_IP_ROUTE][device_type])
    print(output)

    print(f"\n\n----- showing arp table for {device_type} -------------------")
    output = connection.send_command(commands[SHOW_ARP][device_type])
    print(output)

    print(f"\n\n----- showing interface description for {device_type} -------------------")
    output = connection.send_command(commands[SHOW_INT_DESCRIPTION][device_type])
    print(output)

    print(f"\n\n----- showing interface brief for {device_type} -------------------")
    output = connection.send_command(commands[SHOW_INT_BRIEF][device_type])
    print(output)

    connection.disconnect()

The netmiko_connect() function takes a device_type argument, uses the connection details from the cisco_sandbox_devices dictionary to establish an SSH connection to the device using Netmiko, and returns the connection object.

The disconnect() function takes a connection argument and uses the disconnect() method from Netmiko to close the SSH connection.

Several constants are defined that represent Cisco IOS CLI commands, and a dictionary called commands maps these commands to their equivalent Cisco IOS CLI commands.

The script then iterates over the commands dictionary, establishing a connection to the Cisco IOS device using netmiko_connect(), and using the send_command() method of the connection object to send each command and print the resulting output to the console. Finally, the connection is closed using the disconnect() function.