Python for network engineers: libraries and a first automation script
In short: Python is easy to read and has libraries built for network engineers. With Netmiko you can log in to every device, collect or change configuration and save the results. Start with a nightly backup script, keep passwords out of the code, test on a lab first, and track everything in Git.
Why Python
Python is the most widely used language in network automation. It reads almost like English, runs on Windows, macOS and Linux, and has excellent libraries written specifically for network engineers. You don't need to become a software developer: a few hundred lines of Python can save hours every week.
Libraries worth knowing
| Library | Use it for |
|---|---|
| Netmiko | SSH to devices from many vendors, send commands and configuration |
| NAPALM | The same functions across vendors: get facts, compare and replace configs |
| ncclient | NETCONF sessions |
| requests | REST APIs on controllers and cloud services |
| Nornir | Run tasks on many devices in parallel, written in Python |
| TextFSM / TTP | Turn text command output into structured data |
| PyYAML, json | Read and write YAML and JSON files |
A first script: backing up configurations
import datetime
import getpass
import yaml
from netmiko import ConnectHandler
with open("devices.yaml") as f:
devices = yaml.safe_load(f) # list of {name, host, type}
user = input("Username: ")
password = getpass.getpass("Password: ") # never hard-code passwords
today = datetime.date.today().isoformat()
for d in devices:
try:
conn = ConnectHandler(device_type=d["type"], host=d["host"],
username=user, password=password)
config = conn.send_command("show running-config")
conn.disconnect()
with open(f"{d['name']}_{today}.cfg", "w") as out:
out.write(config)
print("Saved", d["name"])
except Exception as err:
print("FAILED", d["name"], err)
The inventory file is plain YAML:
- name: BR1-SW1
host: 192.168.99.11
type: cisco_ios
- name: BR1-RTR
host: 192.168.99.1
type: cisco_ios
Making changes safely
commands = ["vlan 30", "name CAMERAS"]
output = conn.send_config_set(commands) # enters config mode, applies, exits
conn.save_config() # write memory
- Test against a lab or one pilot device first.
- Back up before every change, and check the result afterwards with a show command.
- Keep credentials in environment variables or a vault, never in the script.
- Store scripts in Git so every change is tracked.
How to learn
- Learn Python basics: variables, lists, dictionaries, loops, functions and files.
- Run Python against a free lab (simulators or virtual routers).
- Automate one boring task you really do, such as backups or a port report.
- Learn Git, then try NAPALM, Nornir and APIs.
Real-world example: a technician's first script
An IT technician at a fictional international school looked after 35 switches and spent part of every Friday logging in to each one to save its configuration to a shared folder. Using a lab switch first, he wrote a Python script with Netmiko that read the device list from a YAML file, took the password from an environment variable rather than the code, connected to each switch in turn and saved its running configuration with the date in the file name. He stored the script and the backups in a private Git repository, so he could see exactly what had changed between weeks. The Friday task now runs automatically every night, and when a switch failed after a power cut its replacement was configured from the previous night's backup in minutes.
Practice
Which library would you use to SSH into Cisco, Juniper and Arista devices from one script?
Why does the script use getpass instead of writing the password in the file?
Why wrap each device in try/except?
Key takeaways
- Python plus network libraries covers most automation tasks.
- Netmiko handles SSH; NAPALM gives vendor-neutral functions.
- Keep devices in a YAML inventory and loop over them.
- Never hard-code credentials; test on a lab; back up first.
- Start with backups and reports, then move to changes.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.