Step-by-Step: Automating Router Backups with Python
Step-by-Step: Automating Router Backups with Python
Introduction
Backing up router configurations is critical in telecom and enterprise networks. Traditionally, engineers log in manually and copy configs — a slow, error-prone process. With Python + Netmiko, we can automate backups for multiple routers in just a few lines of code.
Requirements
- Python 3 installed on your system.
- Netmiko library (
pip install netmiko). - Router IP, username, and password.
- A text editor or IDE (VS Code, PyCharm, etc.).
Step 1: Install Netmiko
pip install netmiko
Step 2: Create a Python Script
Here’s a simple example for a Cisco router:
from netmiko import ConnectHandler
from datetime import datetime
# Device info (edit to your environment)
device = {
"device_type": "cisco_ios",
"host": "192.168.1.1",
"username": "admin",
"password": "password",
}
# Connect to router
net_connect = ConnectHandler(**device)
# Get running-config
output = net_connect.send_command("show running-config")
# Save with timestamp
filename = f"backup_{device['host']}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
with open(filename, "w") as f:
f.write(output)
print(f"Backup saved as {filename}")
# Close connection
net_connect.disconnect()
Step 3: Run and Verify
Run the script, and a backup file will be saved with a timestamp. You can extend this by looping through a list of devices (Huawei, Juniper, ZTE, etc.).
Conclusion
Automating router backups with Python saves time, reduces human error, and ensures configs are always available in case of failure. This is a must-have skill for network engineers in Bangladesh as telecom networks grow in scale.
Comments
Post a Comment