Octet

What is Netmiko, and how does a basic Netmiko script work?

Show the answer

Say this first

Netmiko is a Python library built on Paramiko that makes SSH to network devices easy. It knows each platform's prompts, disables paging, enters enable and config mode, and waits for output. You build a device dictionary with a device_type such as cisco_ios, open ConnectHandler, then call send_command for show commands and send_config_set for changes. It is screen scraping: reliable and universal, but you get text back unless you parse it.

Go deeper

A minimal script

Python
from netmiko import ConnectHandler

device = {
    "device_type": "cisco_ios",
    "host": "10.0.0.11",
    "username": "netops",
    "password": "secret",
    "secret": "enablepass",
}

with ConnectHandler(**device) as conn:
    conn.enable()
    print(conn.send_command("show ip interface brief"))
    output = conn.send_config_set([
        "interface Loopback100",
        "description mgmt-test",
    ])
    print(output)
    conn.save_config()

The methods you use most

MethodWhat it does
send_command(cmd)Send one command, wait for the prompt, return the output
send_command(cmd, use_textfsm=True)Parse the output with a TextFSM template into a list of dictionaries
send_config_set(list)Enter config mode, send the lines, exit
send_config_from_file(path)Same, from a file
save_config()Platform-appropriate save (write memory on IOS)
enable()Enter privileged mode using secret

Common device types

cisco_ios, cisco_xe, cisco_nxos, cisco_xr, arista_eos, juniper_junos, linux.

Timeouts

In Netmiko 4, send_command waits up to read_timeout (10 seconds by default) for the prompt. A big show running-config on a core switch can need read_timeout=60.

Traps

  • Calling Netmiko an API. It types commands into an SSH session like a person would.
  • Parsing output with hand-written string splits that break on the next software release.
How did you do?