Lesson 2 of 10
Backup All Your Device Configs
The Scenario
You have 20 switches and routers. You back up configs manually by SSHing into each one, running show running-config, and pasting into a text file. This takes 30+ minutes and you forget to do it half the time. One script solves this forever.
The Prompt
Write a Python script using Netmiko that:
1. Reads a list of devices from a CSV file (columns: hostname, ip, device_type, username, password)
2. SSHs into each device
3. Runs "show running-config"
4. Saves the output to a file named [hostname]_[date].txt in a "backups" folder
5. Prints a summary: which devices succeeded and which failed
6. Handles connection timeouts gracefully (do not crash if one device is down)
Device types are cisco_ios for switches and cisco_xe for routers.
The CSV File (devices.csv)
hostname,ip,device_type,username,password
SW-CORE01,10.1.1.1,cisco_ios,admin,SecurePass1
SW-ACCESS01,10.1.1.2,cisco_ios,admin,SecurePass1
RTR-WAN01,10.1.1.3,cisco_xe,admin,SecurePass1
What AI Gives You
A complete script that loops through the CSV, connects to each device, grabs the config, and saves it with a timestamp. Handles errors per-device so one timeout does not kill the whole run.
Review and Validate
- Security: Never hardcode passwords in production scripts. AI might do this — ask it to "use environment variables or getpass instead"
- Device types: Netmiko device types must be exact.
cisco_iosfor IOS,cisco_xefor IOS-XE,cisco_nxosfor Nexus - SSH keys: For production, ask AI to modify the script to use SSH key authentication instead of passwords
Try It Yourself
Create a CSV with 3 devices you have access to. Run the script. Check the backups folder. Schedule it with cron (Linux) or Task Scheduler (Windows) to run daily.