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:

  1. Explain the role of Python in network automation.

  2. Understand Python syntax, variables, and data types.

  3. Work with strings, lists, tuples, dictionaries, and sets.

  4. Use conditional statements, loops, and functions.

  5. Read and write files using Python.

  6. Understand JSON syntax and data structures.

  7. Convert Python dictionaries into JSON and JSON into Python objects.

  8. Parse network device information stored in JSON.

  9. Automate Cisco device access using SSH.

  10. Retrieve network information using REST APIs.

  11. Handle errors and exceptions in automation scripts.

  12. Develop basic network configuration backup and validation scripts.

1. What Is Python?

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.

Network Automation Architecture
Pasted content image

Common Python Libraries

Library

Purpose

netmiko

SSH automation for network devices

paramiko

SSH connectivity and operations

requests

HTTP and REST API communication

json

JSON encoding and decoding

ipaddress

IP address and subnet calculations

re

Regular expression processing

csv

CSV file operations

pathlib

File and directory management

logging

Application logging

concurrent.futures

Concurrent task execution

ncclient

NETCONF automation

Some libraries are included with Python, while others require installation.


1.2 Lab Topology - Used Throughout This Python Module

Pasted content image

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…