online posts 16
lessons

Python 101 for network engineers

Enough Python to automate a network: run code and keep packages in a venv, turn show output into lists and dicts, read files and survive bad ones, parse with regex and TextFSM, log in with Netmiko, read YAML, JSON and a REST API, and ship a small inventory tool. Eight lessons, each with a hands-on lab chapter.

Python 101 for network engineers

Every network automation tool you’ll meet after this, Ansible, NetBox scripts, pyATS, ANTA, an MCP server, is either written in Python or reads data shaped the way Python shapes it: lists, dicts, YAML, JSON. This course teaches that much Python, and no more, using the things you already look at every day: show output, saved configs and device inventories.

It starts from nothing. Lesson 1 runs your first line of code. By lesson 8 you’ve written a script that logs in to every device in an inventory, parses show version, writes a CSV, and says which devices it couldn’t reach. In between: strings, lists and dicts; loops, conditions and functions; files and errors; regular expressions and TextFSM; Netmiko; YAML, JSON and a REST API.

Before you start: Linux and the command line 101 (a shell, files, environment variables and SSH, which every lab here uses) and Networking 101 (interfaces, addresses and what a router does). No programming experience.

Every lesson comes with a chapter of the hands-on lab: three routers and a switch running CapeOS, a small inventory API, and a checker that runs your functions on inputs they’ve never seen. The tools are the ones the industry uses, unchanged: Netmiko, TextFSM with ntc-templates, PyYAML and requests.

The course stops before Nornir and pyATS, the frameworks that run this kind of code across hundreds of devices at once. Lesson 8 shows what Nornir would add to the script you’ve written, and pyATS is part of network testing.

  1. 1 10 min · lab chapter 1, 2 tasks Running Python Three ways to run Python, where pip puts packages, why every project gets its own venv, what externally-managed-environment means, and how requirements.txt pins versions.
  2. 2 13 min · lab chapter 2, 2 tasks Strings, lists and dicts Everything a device prints reaches Python as a string. How split, strip, slicing and f-strings take a line of show output apart and put it back, what lists and dicts are, …
  3. 3 11 min · lab chapter 3, 2 tasks Loops, conditionals and functions A for loop runs the same lines for every interface, if decides which ones matter, and a function wraps it up so it can be reused and tested. for over lists and dicts, …
  4. 4 12 min · lab chapter 4, 2 tasks Files, modules and errors Read a folder of saved configs with pathlib and with, write a CSV report, split the code into a module and a script, read a traceback from the bottom up, and catch only …
  5. 5 11 min · lab chapter 5, 2 tasks Regex and TextFSM Turn show ip interface brief into a list of dicts twice, with a regular expression and with an ntc-templates TextFSM template, see how a template gets picked, and when …
  6. 6 12 min · lab chapter 6, 2 tasks Talking to devices with Netmiko Log in to a router from Python with Netmiko, run show commands and get them back parsed, push a config change, keep the password in .env instead of the code, and turn a …
  7. 7 12 min · lab chapter 7, 2 tasks YAML, JSON and a first REST call YAML and JSON are two ways of writing the same lists and dicts. Load an inventory with yaml.safe_load, meet the values YAML reads differently from you, round-trip it …
  8. 8 13 min · lab chapter 8, 3 tasks Capstone: an inventory to CSV Shape a small network tool: an inventory in, one function per step, a CSV out. Two device types into one row, csv.DictWriter, carrying on when a device is down, exit …