← back to blog

// automation · python · networking

Getting Started with Network Automation Using Python

#automation#python#networking

If you've spent any time in networking, you know the drill. Twenty switches need an SNMP community string updated. You SSH into each one, paste the same three commands, check the output, move on. An hour gone for a five-second change.

Python fixes that. Here's how I started.

The setup

You'll need two libraries:

pip install netmiko paramiko

Netmiko is the workhorse — it handles SSH connections to network devices and normalises the quirks between vendors. Paramiko is the underlying SSH library it builds on.

Connecting to a single device

Start simple. One device, one command:

from netmiko import ConnectHandler

device = {
    'device_type': 'cisco_ios',
    'host': '192.168.1.1',
    'username': 'admin',
    'password': 'your_password',
}

with ConnectHandler(**device) as conn:
    output = conn.send_command('show version')
    print(output)

The with block handles the connection teardown cleanly. send_command waits for the prompt to return — you get the full output as a string.

Scaling to a device list

Now the useful bit. Pull your device list from a file and loop:

from netmiko import ConnectHandler
import json

with open('devices.json') as f:
    devices = json.load(f)

commands = [
    'snmp-server community NEW_COMMUNITY RO',
    'no snmp-server community OLD_COMMUNITY',
]

for device in devices:
    print(f"Connecting to {device['host']}...")
    try:
        with ConnectHandler(**device) as conn:
            conn.enable()
            output = conn.send_config_set(commands)
            print(output)
    except Exception as e:
        print(f"Failed on {device['host']}: {e}")

send_config_set enters config mode, sends your list of commands line by line, and exits config mode. One call, all the work done.

The devices.json format

[
  {
    "device_type": "cisco_ios",
    "host": "192.168.1.1",
    "username": "admin",
    "password": "secret"
  },
  {
    "device_type": "cisco_ios",
    "host": "192.168.1.2",
    "username": "admin",
    "password": "secret"
  }
]

In production you'd pull credentials from a secrets manager rather than a JSON file, but this is the shape.

What's next

This is the foundation. Once you're comfortable with this pattern, the logical next steps are:

  • Concurrent connectionsconcurrent.futures to hit 20 devices simultaneously instead of sequentially
  • Ansible — for larger environments where you want idempotency and a proper inventory system
  • Nornir — a Python automation framework designed specifically for networking, more powerful than raw Netmiko loops

The key insight is that every manual CLI task is a candidate for automation. Start with the things you do most often.