Advanced Network Automation using Python
- Enterprise Lab Guide
This advanced lab guide is designed for Network Engineers, CCNP/DevNet learners, and Enterprise Automation
Engineers. The labs cover enterprise-level automation using Netmiko, Paramiko, Jinja2, NAPALM, REST APIs,
TextFSM, YAML inventories, and Email Automation.
Lab Topic
1 Enterprise SSH Automation
2 Multi-Device Configuration Push
3 Configuration Backup Framework
4 YAML-Based Inventory Automation
5 Jinja2 Enterprise Templates
6 Network Health Check Automation
7 REST API Automation
8 TextFSM Parsing
9 Email Reporting Automation
10 Threading for Parallel Automation
Lab 1 - Enterprise SSH Automation
Objective: Connect securely to Cisco devices and execute operational commands.
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "[Link]",
"username": "admin",
"password": "cisco"
}
connection = ConnectHandler(**device)
output = connection.send_command("show version")
print(output)
[Link]()
Lab 2 - Multi-Device Configuration Push
Objective: Deploy configurations to multiple devices simultaneously.
from netmiko import ConnectHandler
devices = ["[Link]", "[Link]"]
commands = [
"vlan 100",
"name AUTOMATION"
]
for ip in devices:
device = {
"device_type": "cisco_ios",
"host": ip,
"username": "admin",
"password": "cisco"
}
conn = ConnectHandler(**device)
output = conn.send_config_set(commands)
print(output)
[Link]()
Lab 3 - Enterprise Backup Framework
Objective: Backup configurations with timestamp and structured folders.
from netmiko import ConnectHandler
from datetime import datetime
import os
today = [Link]().strftime("%Y-%m-%d")
[Link](today, exist_ok=True)
device = {
"device_type": "cisco_ios",
"host": "[Link]",
"username": "admin",
"password": "cisco"
}
conn = ConnectHandler(**device)
config = conn.send_command("show running-config")
with open(f"{today}/[Link]", "w") as file:
[Link](config)
[Link]()
Lab 4 - YAML Inventory Automation
Objective: Store device inventory in YAML files for scalability.
import yaml
with open("[Link]") as file:
devices = yaml.safe_load(file)
print(devices)
Lab 5 - Jinja2 Enterprise Templates
Objective: Generate reusable enterprise network configurations.
from jinja2 import Template
template = '''
hostname {{ hostname }}
router ospf 1
network {{ network }} area 0
'''
data = {
"hostname": "R1",
"network": "[Link] [Link]"
}
j2 = Template(template)
config = [Link](data)
print(config)
Lab 6 - Network Health Check Automation
Objective: Verify interface status and CPU utilization.
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "[Link]",
"username": "admin",
"password": "cisco"
}
conn = ConnectHandler(**device)
print(conn.send_command("show interfaces status"))
print(conn.send_command("show processes cpu"))
[Link]()
Lab 7 - REST API Automation
Objective: Use Python requests module for API-based automation.
import requests
url = "[Link]
response = [Link](url, auth=("devnetuser", "Cisco123!"))
print([Link]())
Lab 8 - TextFSM Parsing
Objective: Parse CLI outputs into structured data.
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "[Link]",
"username": "admin",
"password": "cisco"
}
conn = ConnectHandler(**device)
output = conn.send_command(
"show ip interface brief",
use_textfsm=True
)
print(output)
[Link]()
Lab 9 - Email Reporting Automation
Objective: Automatically send network reports through email.
import smtplib
from [Link] import MIMEText
msg = MIMEText("Backup Completed Successfully")
msg["Subject"] = "Automation Report"
server = [Link]("[Link]", 587)
[Link]()
[Link]("your_email@[Link]", "app_password")
[Link](
"your_email@[Link]",
"admin@[Link]",
msg.as_string()
)
[Link]()
Lab 10 - Parallel Automation using Threading
Objective: Speed up automation using Python threading.
from [Link] import ThreadPoolExecutor
devices = ["[Link]", "[Link]"]
def connect_device(ip):
print(f"Connecting to {ip}")
with ThreadPoolExecutor(max_workers=5) as executor:
[Link](connect_device, devices)
Enterprise Best Practices
• Use encrypted credential vaults
• Store inventory in YAML/CSV/Database
• Use logging for troubleshooting
• Validate configurations before deployment
• Use Git for configuration version control
• Implement rollback procedures
• Schedule backups using Cron or Task Scheduler
• Integrate monitoring and alerting systems
Recommended Python Libraries
pip install netmiko
pip install paramiko
pip install napalm
pip install jinja2
pip install pyyaml
pip install requests