Automate your network with Python and Netmiko

The gateway drug into network automation is almost always the same task: you need to run one command, or one small set of commands, across a pile of devices, and doing it by hand over SSH is starting to hurt. That's exactly what Netmiko was built for.

Why Netmiko instead of raw SSH libraries

Vendor CLIs are interactive by design — prompts, --More-- pagination, enable mode, different prompt characters per platform. A raw SSH library hands you a shell and makes you handle all of that yourself. Netmiko already knows the quirks of dozens of platforms (Cisco IOS, NX-OS, Juniper, Arista, and more) and gives you a simple send_command / send_config_set interface that behaves the same regardless of vendor.

A first script that's actually useful

A realistic starting point isn't "automate everything" — it's "collect show version from every device in inventory and flag anything running an unexpected image." That one script alone catches drift that's easy to miss manually: a device that didn't get the last upgrade, a device someone reloaded onto the wrong image after a maintenance window.

from netmiko import ConnectHandler

devices = [
    {"device_type": "cisco_ios", "host": "10.0.0.1", "username": "admin", "password": "..."},
    {"device_type": "cisco_ios", "host": "10.0.0.2", "username": "admin", "password": "..."},
]

for device in devices:
    conn = ConnectHandler(**device)
    output = conn.send_command("show version")
    print(device["host"], "->", output.splitlines()[0])
    conn.disconnect()

Where to go from "it works on my laptop"

Once a script like this proves useful, the next steps are usually: move credentials out of the script into a vault or environment variables, load the device list from an inventory file instead of a hardcoded list, and add basic error handling so one unreachable device doesn't kill the whole run. None of that is glamorous, but it's the difference between a script you run once and a tool your team actually relies on.