Lesson Overview
Modern enterprise networks contain hundreds or thousands of routers, switches, firewalls, and other network devices. Managing these devices manually through the command-line interface (CLI) can be time-consuming, repetitive, and prone to human error.
Python is one of the most widely used programming languages for network automation because of its readable syntax, extensive libraries, and integration capabilities.
Network engineers can use Python to automate common operational tasks such as collecting device information, backing up configurations, checking interface status, validating network connectivity, and interacting with network controllers.
JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used by REST APIs, network controllers, and automation tools. Understanding JSON allows network engineers to exchange structured data between scripts, devices, and management platforms.
This lesson introduces Python programming concepts and explains how Python and JSON work together in practical network automation scenarios.
Learning Objectives
After completing this lesson, you will be able to:
Explain the role of Python in network automation.
Understand Python syntax, variables, and data types.
Work with strings, lists, tuples, dictionaries, and sets.
Use conditional statements, loops, and functions.
Read and write files using Python.
Understand JSON syntax and data structures.
Convert Python dictionaries into JSON and JSON into Python objects.
Parse network device information stored in JSON.
Automate Cisco device access using SSH.
Retrieve network information using REST APIs.
Handle errors and exceptions in automation scripts.
Develop basic network configuration backup and validation scripts.
Python is a high-level, general-purpose programming language used for automation, application development, data analysis, artificial intelligence, and network management. Python is particularly useful for network engineers because it allows repetitive tasks to be performed programmatically.
Instead of logging in to every router or switch manually, an engineer can create a Python script that connects to multiple devices, executes commands, collects output, and generates reports.
Manual vs Automated Network Management
Task | Manual Approach | Python Automation |
|---|---|---|
Check interface status | Log in to each device | Collect status automatically |
Configuration backup | Copy configuration manually | Save configurations using a script |
Device inventory | Record details in a spreadsheet | Generate inventory automatically |
Network monitoring | Run CLI commands repeatedly | Collect data periodically |
Configuration validation | Compare configurations manually | Validate configuration rules |
REST API interaction | Use GUI or API client | Send requests using Python |
Benefits of Python
Easy to learn and understand.
Supports multiple operating systems.
Provides extensive libraries.
Integrates with SSH and REST APIs.
Supports structured data formats such as JSON and YAML.
Reduces repetitive manual operations.
Improves consistency and operational efficiency.
Supports large-scale network automation.
1.1 Python in Network Automation
Python can interact with network devices using several protocols and interfaces.
Common examples include:
SSH.
REST APIs.
NETCONF.
RESTCONF.
SNMP.
Vendor-specific SDKs.
Common Python Libraries
Library | Purpose |
|---|---|
| SSH automation for network devices |
| SSH connectivity and operations |
| HTTP and REST API communication |
| JSON encoding and decoding |
| IP address and subnet calculations |
| Regular expression processing |
| CSV file operations |
| File and directory management |
| Application logging |
| Concurrent task execution |
| NETCONF automation |
Some libraries are included with Python, while others require installation.
1.2 Lab Topology - Used Throughout This Python Module
Figure 1.2 - The same management topology, IP plan, SSH policy, and Netmiko connection logic are reused in every example.
|
Node |
IP address |
Purpose |
|
Automation PC |
192.168.10.10/24 |
Python 3 + Netmiko. Runs all scripts. |
|
SW1 |
Layer 2 / VLAN 10 |
Places the automation PC and router management interfaces in the same management LAN. |
|
R1 |
192.168.10.1/24 |
Primary device for single-device SSH examples. |
|
R2 |
192.168.10.2/24 |
Second device for lists/loops examples. |
|
R3 |
192.168.10.3/24 |
Third device for lists/loops and output comparison. |
1.3 IOS XE Configuration - Enable SSH on the Same Topology
|
The examples below use a lab local account. In production, prefer AAA/RBAC and an approved credential vault. Do not hard-code real passwords in Python files. |
R1 - SSH configuration
|
hostname R1 ! interface GigabitEthernet0/0 ip address 192.168.10.1 255.255.255.0 no shutdown ! ip domain name lab.local username netadmin privilege 15 secret <PASSWORD> ! crypto key generate rsa modulus 2048 ip ssh version 2 ! line vty 0 4 login local transport input ssh exec-timeout 10 0 |
R2 - same policy, unique hostname/IP
R3 - same policy, unique hostname/IP
Verify SSH Before Writing Python
Healthy output…
