Back to Blog
โ˜…โ˜…โ˜†Intermediate๐Ÿค– Network Automation
PythonNetmikoAutomationNetwork Engineering

Automating Network Configuration with Python and Netmiko

May 20, 2024ยท2 min read

The Problem

Manual network configuration is error-prone and slow. Before automation, deploying an ACL change to 50 Cisco devices took 4+ hours of repetitive copy-paste work. A single typo could bring down a critical link.

I built a Python automation framework using Netmiko that transformed this process.

What is Netmiko?

Netmiko is a Python library that simplifies SSH connections to network devices. It supports virtually every major vendor: Cisco IOS/IOS-XE/NX-OS, Palo Alto, Juniper, Arista, and more.

Basic Connection Example

python
from netmiko import ConnectHandlerdevice = {    'device_type': 'cisco_ios',    'host': '192.168.1.1',    'username': 'admin',    'password': 'secret',    'secret': 'enable_secret',}with ConnectHandler(**device) as net_connect:    net_connect.enable()    output = net_connect.send_command('show ip bgp summary')    print(output)

Mass Configuration Deployment

Here's a simplified version of the script I use for mass deployments:

python
import csvfrom netmiko import ConnectHandlerfrom netmiko.exceptions import NetmikoTimeoutExceptionfrom jinja2 import Templateimport logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)def deploy_config(device_info: dict, commands: list[str]) -> dict:    result = {'host': device_info['host'], 'status': 'unknown', 'output': ''}    try:        with ConnectHandler(**device_info) as conn:            conn.enable()            output = conn.send_config_set(commands)            conn.save_config()            result['status'] = 'success'            result['output'] = output    except NetmikoTimeoutException:        result['status'] = 'timeout'        logger.error(f"Timeout connecting to {device_info['host']}")    except Exception as e:        result['status'] = 'error'        result['output'] = str(e)        logger.error(f"Error on {device_info['host']}: {e}")    return result

Results

After implementing this framework:

  • 70% reduction in configuration deployment time
  • Zero errors in 200+ deployments since go-live
  • Audit trail for every change with timestamped logs
  • Compliance reports generated automatically

Key Takeaways

Network automation isn't about replacing network engineers โ€” it's about amplifying their capabilities. With Netmiko and Python, you can focus on architecture and design instead of repetitive operational tasks.

In a future post, I'll cover how to integrate Netmiko with ServiceNow for change-driven automation workflows.