Let Python do the boring bits
Automate it once, never do it again. That's the dream.
Logging into routers one by one to save their configs gets old fast. With the Netmiko library, a dozen lines of Python do it for you, for two devices or two hundred.
๐งฐ What you need
- Python 3 on your computer
- Lab routers you can SSH to, in CML, GNS3 or EVE-NG. Packet Tracer devices can't be reached from real Python
Let's build it
Make sure each router accepts SSH
On each router's console (use your own hostname and password):
configure terminal
hostname R1
ip domain name lab.local
username admin privilege 15 secret LabPass123
crypto key generate rsa modulus 2048
ip ssh version 2
line vty 0 4
login local
transport input ssh
endCheck it works from your computer with ssh admin@<router-ip>.
Set up Python
mkdir backups && cd backups
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install netmikoWrite the script
Save this as backup.py, putting your routers' IPs in the list:
from datetime import date
from getpass import getpass
from netmiko import ConnectHandler
devices = ["192.168.100.11", "192.168.100.12"]
username = input("Username: ")
password = getpass()
for host in devices:
with ConnectHandler(device_type="cisco_ios", host=host,
username=username, password=password) as conn:
hostname = conn.find_prompt().strip("#>")
config = conn.send_command("show running-config")
filename = f"{hostname}_{date.today()}.txt"
with open(filename, "w") as f:
f.write(config)
print(f"Saved {filename}")Run it
python3 backup.pyโ How you know it worked
You get one file per router, like R1_2026-09-27.txt, containing its full running configuration.
๐ฅ Break it on purpose
Add an IP that doesn't exist to the list and run it again. The script crashes on that device and never reaches the rest. Your challenge: wrap the with ConnectHandler(...) block in try: / except Exception as e: so it prints a warning and moves on. That's the difference between a script and a tool.
๐ง What's really going on
Netmiko opens an SSH session exactly as you would, waits for the prompt, sends the command and collects the output, dealing with paging and prompts for you. The password is typed at run time with getpass, so it never sits in the file.
โ Back to all network labs ยท Stuck? Email me