Enterprise Cisco Switch Backup Automation
Work & Enterprise Project | Python & Netmiko Multi-Vendor Network Configuration Backup Engine
A production-grade Python automation script engineered to dynamically log into a mixed network environment consisting of Cisco Catalyst Core switches and Cisco Small Business (SG300) edge switches. The script handles legacy SSH cryptography bugs, dynamic device drivers, and extended timeout bottlenecks to generate automated date-stamped backup archives for disaster recovery.
How it Works
Pipeline Architecture
How the automation process works end-to-end
Automation Script
A single Python script securely logs into the network using correct credentials for each specific device type.
Network Switches
Safely downloads configurations from both Core and Edge switches, bypassing legacy connection errors.
Organized Storage
Saves all backups into neat, date-stamped folders ready for immediate disaster recovery and audits.
Technical Challenges & Bottlenecks
Different Types of Network Switches
The network had a mix of older and newer Cisco switches. The older ones asked for login credentials in a slightly different format, causing standard automation scripts to get confused and fail.
Connection Security Issues
Some older switches had outdated security protocols that modern automation tools refused to connect to, causing instant connection failures before a backup could even start.
Slow Response Times
Older switches took a long time (up to 40 seconds) to generate their backup files. Standard scripts would get impatient and cancel the backup prematurely.
The Engineered Solutions
Smart Device Recognition
- Created a system that automatically identifies the type of switch before logging in.
- Applies the exact right connection method and credentials needed for that specific device type.
Secure Connection Fallbacks
- Built a secure workaround to successfully connect to older switches despite their outdated security protocols.
- Ensured newer switches still used the highest level of modern security (SSH v2).
Patience and Reliability
- Programmed the script to be patient, giving slow switches plenty of time (up to 90 seconds) to finish generating their backups without crashing.
- Ensured the connection stays stable even on slow or delayed network links.
Automated Organization
- Script automatically creates neat, date-stamped folders for every single backup run.
- Saves both current and startup configurations into text files, perfectly organized and ready for instant disaster recovery.
Automated Switch Backup Script (`cisco_backup.py`)
import os
from netmiko import ConnectHandler
from datetime import datetime
# 1. Dynamic Device Mapping (IP to Credentials & Netmiko Device Type)
# .16 = Catalyst Core Switch (cisco_ios over SSH)
# .17, .18, .20 = Edge Small Business Switches (cisco_s300 over Telnet/VPN)
switches = {
"192.168.8.16": {"password": "********", "device_type": "cisco_ios"},
"192.168.8.17": {"password": "********", "device_type": "cisco_s300_telnet"},
"192.168.8.18": {"password": "********", "device_type": "cisco_s300_telnet"},
"192.168.8.20": {"password": "********", "device_type": "cisco_s300_telnet"},
}
USERNAME = "admin"
# 2. Generate Date-Stamped Enterprise Directory
date_today = datetime.now().strftime("%Y-%m-%d")
backup_dir = f"backups/{date_today}"
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
# 3. Automation Execution Loop
for ip, config in switches.items():
print(f"
Connecting to switch: {ip}...")
# Define connection parameters dynamically
cisco_device = {
'device_type': config["device_type"],
'host': ip,
'username': USERNAME,
'password': config["password"],
'auth_timeout': 60,
'allow_agent': False, # Bypasses Windows SSH Agent conflicts
'use_keys': False, # Forces raw password authentication
}
try:
net_connect = ConnectHandler(**cisco_device)
# Pull Running Config with extended 90s timeout for slow SMB switches
print(f" -> Downloading running-config...")
running_config = net_connect.send_command("show running-config", read_timeout=90)
with open(f"{backup_dir}/{ip}_running-config.txt", "w") as f:
f.write(running_config)
# Pull Startup Config
print(f" -> Downloading startup-config...")
startup_config = net_connect.send_command("show startup-config", read_timeout=90)
with open(f"{backup_dir}/{ip}_startup-config.txt", "w") as f:
f.write(startup_config)
print(f"✅ Success! Backups saved inside {backup_dir}/")
net_connect.disconnect()
except Exception as e:
print(f"❌ Failed to backup {ip}. Error: {e}")
print("
🎉 All backups completed and organized!")