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
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
| Method | What 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.
They will ask next
Netmiko or NAPALM or Nornir?
Netmiko is the SSH layer: any command on almost any platform. NAPALM adds a vendor-neutral API with structured getters and config replace or merge with diffs. Nornir is a Python framework that runs tasks (Netmiko, NAPALM, others) across an inventory in parallel. They are often used together.
How do you turn CLI output into data?
TextFSM templates (the ntc-templates collection ships hundreds) through
use_textfsm=True, Cisco Genie parsers throughuse_genie=True, or TTP templates. Better still, ask for structured output where the platform offers it, such as| jsonon NX-OS and EOS or| display jsonon Junos.
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.