Free tech learning for everyone, from basics to pro

Network AutomationNetworking SecurityIntermediate

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

LibraryUse it for
NetmikoSSH to devices from many vendors, send commands and configuration
NAPALMThe same functions across vendors: get facts, compare and replace configs
ncclientNETCONF sessions
requestsREST APIs on controllers and cloud services
NornirRun tasks on many devices in parallel, written in Python
TextFSM / TTPTurn text command output into structured data
PyYAML, jsonRead and write YAML and JSON files

A first script: backing up configurations

A Python backup script, step by stepRead inventorydevices.yamlConnectSSH with NetmikoRun commandshow running-configSave fileone per device, datedCommitto Git, see changesLoop over every device; record failures and carry onSchedule it nightly and you always have yesterday’s configuration
Figure 1. A first useful script: back up every device’s configuration automatically.
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

  1. Learn Python basics: variables, lists, dictionaries, loops, functions and files.
  2. Run Python against a free lab (simulators or virtual routers).
  3. Automate one boring task you really do, such as backups or a port report.
  4. 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?
Netmiko (or NAPALM for vendor-neutral functions).
Why does the script use getpass instead of writing the password in the file?
So the password is never stored in the code or in version control.
Why wrap each device in try/except?
So one unreachable device doesn’t stop the backup of all the others.

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.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

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.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.