Script to backup multiple switches

Simple Python script running in PowerShell to backup multiple switches. Very useful in environments without Cisco ISE or backup tools. In our example the command was made to Cisco but you can change to other syntax and upgrade.

AUTOMATIONCISCO

7/26/20262 min read

To perform backups of multiple Cisco switches in an automated and scalable way from a Windows 11 host, the most recommended tool in current network engineering is Python along with the Netmiko library.

Netmiko natively handles the SSH peculiarities of network equipment (such as terminal pagination and privilege elevation via enable), making it ideal for corporate infrastructures and modern architectures based on Cisco IOS/IOS-XE.

Below is a straightforward guide on how to prepare the environment on Windows 11 and the automation script.

1. Environment Preparation (Windows 11)

If you don't have Python installed yet, the fastest way on Windows 11 is using the winget package manager via PowerShell or downloading it from the Microsoft Store.

Open PowerShell and install Python and the Netmiko library:

PowerShell

# Install Python (if you don't have it)
winget install Python.Python.3.11

# After installation, update the Python package manager (pip)
python -m pip install --upgrade pip

# Install Netmiko
pip install netmiko

2. The Inventory File (devices.txt)

Create a plain text file in the same folder where the script will be saved. Place the IP or hostname of each switch on a new line. Do not add extra spaces.

Example of devices.txt in plaintext:

192.168.10.254
192.168.20.10
core-9500-stack.domain.local
access-9200-01.domain.local




3. The Python Backup Script (backup_cisco.py)

This script adopts security best practices: it has no hardcoded passwords. Credentials are securely requested at runtime. It also includes error handling for timeouts or authentication failures, preventing the script from stopping mid-execution if a switch is unreachable.

Download the PDF file from the link below and follow the instructions in the file.

backup_cisco.py:

4. How to Execute

In your Windows 11 terminal (PowerShell or CMD), navigate to the folder where the files were created and run the script:

PowerShell

python backup_cisco.py

5. Results

Notes on Scalability and Security
  • SSH Keys (RSA/Ed25519): If your equipment already has hardening applied and you use public keys instead of passwords, you can modify the device dictionary to point to the key file on Windows by adding 'use_keys': True and 'key_file': 'C:\\Path\\To\\Your\\Key'.

  • Performance: For massive networks (hundreds of switches), this script runs sequentially. For time optimization in large infrastructures, parallel execution can be implemented using Python's native concurrent.futures library or more robust automation frameworks like Nornir.

techcyber.blog

All rights reserved.