Hands-on tutorial
Tutorials · DevOps & Containers
How to Automate Switch Backups and Checks With Python and Netmiko
Write a small Python script that logs in to switches over SSH, saves running configurations with timestamps, runs show commands and reports failures, then schedule it securely.
Before you start
- Python 3 on a management server
- SSH access to switches with a read-only or backup account
- A Git repository or backup folder
Step by step
Install Netmiko
Create a virtual environment and install the library.
python3 -m venv venv && . venv/bin/activate pip install netmiko
Create an inventory
List devices with their address and device type in a YAML or CSV file. Keep credentials out of the file; read them from environment variables or a vault.
Write the backup script
Connect to each device, run the command that shows the running configuration, and save it with the device name and date.
from netmiko import ConnectHandler import os, datetime for dev in inventory: conn = ConnectHandler(device_type=dev['type'], host=dev['host'], username=os.environ['NET_USER'], password=os.environ['NET_PASS']) cfg = conn.send_command('show running-config') open(f"backups/{dev['name']}.cfg", 'w').write(cfg) conn.disconnect()
Handle failures
Catch connection and authentication errors per device so one failure does not stop the run, and list failed devices at the end.
Commit to Git
Commit backups to a Git repository so you can see what changed and when.
Schedule it
Run the script nightly with cron or a systemd timer under a dedicated user.
Add checks
Extend the script with show commands, such as interface errors or PoE status, and send a summary by email or webhook.
Commands are generic, industry-standard examples. Exact syntax and menu names vary by model and firmware; check your device's manual on the Documentation page.
Pro tips
- Use a dedicated read-only account for backups.
- Protect the backup repository: configurations contain sensitive data.
- Test restores from backups occasionally.
FAQ
Frequently asked questions
Netmiko or Ansible?
Netmiko is a simple Python library for scripts; Ansible adds inventory, idempotent tasks and playbooks for larger automation.
Do I need the vendor's API?
No. Netmiko works over SSH using the same commands you type, which suits mixed environments.
Keep learning
Related guides
Back up configurations with Git
Read more →Automate switches with Ansible
Read more →SSH key authentication
Read more →How to Use SSH Keys Instead of Passwords for Network Devices and Servers
Passwords can be guessed, phished and reused. Create an SSH key pair, install the public key on gateways, edge servers and Linux hosts, then turn password logins off.
Read more →How to Reach Remote Sites Safely With an SSH Jump Host
Do not expose SSH on every device. Put one hardened jump host (bastion) in front of the management network and reach everything else through it.
Read more →How to Open a Device Web Page Through an SSH Tunnel
Reach the web interface of a switch, NVR or container on a remote site without opening a firewall port: forward it through SSH.
Read more →Stuck on a step?
Ask Navijin technical support or an authorised partner for help with your configuration.