ArunNetworkingPro
๐Ÿ

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.

Intermediate1 eveningPython
R1R2R3๐Ÿbackup.py๐Ÿ“I got this. Go make tea โ˜•

๐Ÿงฐ What you need

Let's build it

1

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
end

Check it works from your computer with ssh admin@<router-ip>.

2

Set up Python

mkdir backups && cd backups
python3 -m venv venv
source venv/bin/activate      # Windows: venv\Scripts\activate
pip install netmiko
3

Write 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}")
4

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