Network Automation with Python and Netmiko
This post introduces network engineers to automating CLI tasks using Python and Netmiko. It covers connecting to devices, sending show commands, pushing configuration changes, and scaling scripts across multiple devices with practical code examples throughout.
If you've ever found yourself typing the same commands across dozens of switches or routers, you already understand why network automation matters. Repetitive tasks are not just tedious; they're a source of human error. Python, combined with a library called Netmiko, gives you a practical and accessible way to start automating those tasks today.
What Is Netmiko?
Netmiko is an open-source Python library built specifically for interacting with network devices over SSH. It was created by Kirk Byers and builds on top of Paramiko (a lower-level SSH library) to handle the quirks of network device CLIs. The big win with Netmiko is that it supports a wide range of vendors out of the box: Cisco IOS, IOS-XE, NX-OS, Juniper, Arista, and many more.
You install it with a single pip command:
pip install netmikoConnecting to a Device
The core object in Netmiko is ConnectHandler. You pass it a dictionary of device parameters, and it handles the SSH session for you. Here's a basic connection to a Cisco IOS router:
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "192.168.1.1",
"username": "admin",
"password": "cisco123",
"secret": "enable_secret"
}
connection = ConnectHandler(**device)
connection.enable()
output = connection.send_command("show ip interface brief")
print(output)
connection.disconnect()The send_command() method sends a show-style command and waits for the prompt to return before giving you the output. It handles paging automatically, which saves you from dealing with --More-- prompts.
Pushing Configuration Changes
Reading output is useful, but the real power comes from pushing configuration. Netmiko provides send_config_set() for this. It automatically enters and exits config mode for you:
config_commands = [
"interface GigabitEthernet0/1",
"description Uplink to Core",
"no shutdown"
]
output = connection.send_config_set(config_commands)
print(output)You pass a list of commands, and Netmiko handles the rest. This pattern scales easily when you move to looping over multiple devices.
Scaling Across Multiple Devices
Here's where workflow automation really starts to pay off. Suppose you need to update the NTP server on every router in your network. Instead of logging into each one manually, you define your device list and loop through it:
from netmiko import ConnectHandler
devices = [
{"device_type": "cisco_ios", "host": "10.0.0.1", "username": "admin", "password": "cisco123"},
{"device_type": "cisco_ios", "host": "10.0.0.2", "username": "admin", "password": "cisco123"},
{"device_type": "cisco_ios", "host": "10.0.0.3", "username": "admin", "password": "cisco123"},
]
ntp_commands = [
"ntp server 10.10.10.1",
"ntp server 10.10.10.2"
]
for device in devices:
connection = ConnectHandler(**device)
output = connection.send_config_set(ntp_commands)
print(f"Updated {device['host']}:")
print(output)
connection.disconnect()What used to take 15 minutes of manual CLI work now runs in seconds. And critically, every device gets the exact same configuration with no typos or missed steps.
A Few Practical Tips
- Use a secrets manager or environment variables: Never hardcode credentials in your scripts. Use Python's
os.environor a tool like Vault to keep credentials out of your code. - Test in a lab first: Tools like GNS3 or Cisco DevNet Sandboxes let you validate your scripts safely before touching production.
- Log your output: Write the output of each command to a file with timestamps. This gives you an audit trail and helps with troubleshooting.
- Handle exceptions: Wrap your connections in try/except blocks to handle timeouts or authentication failures gracefully without crashing the whole script.
What's Next
You now have a working foundation for Python-based network automation using Netmiko. The next logical step is making your scripts more dynamic by pulling device lists and configuration data from external files, such as CSV or YAML. In the next post, we'll look at how to combine Netmiko with YAML-based inventory files to build more flexible and maintainable automation workflows.