online posts 16
labs

Python 101 for network engineers

Write Python that turns show output into data, then point it at real devices: Netmiko over SSH, TextFSM and ntc-templates, YAML and a REST API, and a script that inventories every device to CSV. Three CapeOS routers, a switch and an inventory API, in your browser, nothing to install.

First lab here? Meet the lab shows how every lab works, once.

Python 101 teaches the Python a network engineer needs. Here you write it, and run it against real devices: three routers, a switch, and a small API that holds the inventory. Every chapter adds a few functions to a Python package of your own, netlab, and the last one puts them together into a tool.

The network

  host1 ── sw1 ── r1 ════ 10.0.12.0/30 ════ r2 ── host2
     192.168.10.0/24  ╲                    ╱  192.168.20.0/24
              10.0.13.0/30 ╲            ╱ 10.0.23.0/30
                             r3

  management network 172.80.111.0/24:  r1 .11   r2 .12   r3 .13   sw1 .21   api .50
NameWhat it isdevice_type
r1, r2routers running CapeOS Boltcisco_ios
sw1a switch running CapeOS Bolt, with VLANs 10, 11, 12 and 20cisco_ios
r3a router running CapeOS Pulse, the other dialect. It joins in chapter 6arista_eos
apithe inventory as a read-only REST API at http://api:8000/api/, answering in NetBox-style JSON
host1, host2two Linux hosts, so the routers have neighbors to show

Your code runs in the codespace and reaches every device over SSH on the management network, by name: r1, r2, r3, sw1 and api resolve from the codespace’s own terminal. The login is in .env.

Why cisco_ios and arista_eos?

CapeOS is an open-source lab network OS with two CLI dialects. Bolt behaves like the CLI Netmiko and ntc-templates call cisco_ios, and Pulse like the one they call arista_eos, closely enough that those libraries work against it unchanged. So this lab uses them exactly as you would at work, and the device_type in the inventory is what picks the dialect.

How each task works

The same five parts as every lab: explain, show, follow along, challenge, then check. Your code goes in netlab/ (one module per chapter) and scripts/, and each task starts at a TODO x.ya comment.

make check is pytest, the test runner most Python projects use. Each task has a file of checks in checks/. They import your functions and call them on inputs you haven’t seen: show outputs made up fresh on every run in the same layout the devices use, other people’s YAML, folders of configs with bad files in them. The live checks compare your results with what the devices say right now. Task 2.1 shows how to read pytest’s output when a check fails.

Stuck? make solve T=<task> puts the solution of one task into your files, only the functions that task asks for, and git diff --staged shows exactly what changed. The solution branch has the finished code, one commit per chapter.

Quick start

  1. Open the lab and sign in with GitHub. The lab is your own copy, in a codespace on your GitHub account's free allowance (what that means). The first start takes a few minutes; coming back takes about one.
  2. The lab opens in your browser: this guide on the left, VS Code on the right, and the network's state at the top. Wait for it to say network up.
  3. Follow task 1.1. Check task, under the guide, tells you when you've got it right.

Finished? Finish lab, top right, deletes the codespace. Left it open? It stops itself after 20 minutes without activity, and Open the lab picks up where you were.

Advanced: run it on your laptop Docker, Git and VS Code
$ git clone https://gitlab.com/networkdoodles-labs/python-101.git
$ cd python-101
$ code .            # then: Reopen in Container
$ make check T=1.1

Same dev container, on your own Docker (4 GB of memory is plenty). No dev container? Linux with Docker, containerlab 0.83 or later, Python 3.11 or later and make: make up builds the venv in .venv.

8 chapters · 17 tasks

ch 1 Running PythonThe REPL, a first script, and the venv the lab runs in. Then a venv of your own, with one pinned package that only it has. 45 min lesson 1
  1. 1.1 Hello, PythonUse the REPL as a calculator, write and run your first script, and find out which Python each terminal runs: the lab’s venv, and the one outside it. 20 min
  2. 1.2 A venv of your ownBuild a venv in scratch/, install one pinned package into it, write its requirements.txt, and prove that the Python outside the venv doesn’t have the package. 25 min
ch 2 Strings, lists and dictsA line of show version into a dict and back, then a whole show ip interface brief into a list of dicts, two-word statuses included. 60 min lesson 2
  1. 2.1 A line of show versionTake the first line of show version apart with string methods, return it as a dict, and put the dict back into a line. On the way, learn to read what make check says when a check fails. 30 min
  2. 2.2 A whole show ip interface briefTurn all of show ip interface brief into a list of dicts, one per interface, by hand with split: skip the header and blank lines, keep administratively down in one piece, and make an unassigned address None. 30 min
ch 3 Loops, conditionals and functionsWhich interfaces are down, how many have each status, and a VLAN list written the way a trunk writes it: 10-12,20. 50 min lesson 3
  1. 3.1 Which interfaces are down?Write two functions that loop over a list of interfaces: one returns the names of those that aren’t working, the other counts how many have each status. 25 min
  2. 3.2 VLAN rangesTurn a list of VLAN numbers into the compact form a trunk’s allowed list uses: [10, 11, 12, 20] becomes "10-12,20". Unsorted, duplicated and empty lists included. 25 min
ch 4 Files, modules and errorsA folder of saved configs into a CSV report, split into a module and a script. Then a file you can’t read and one that isn’t UTF-8: report them, and carry on. 60 min lesson 4
  1. 4.1 Saved configs to a reportRead every saved config in configs/, find out which devices have an NTP server, and write the answer to reports/ntp.csv, with the functions in a module and a given script that calls them. 30 min
  2. 4.2 Survive a bad fileTwo configs in the folder can’t be read: one is locked, one isn’t UTF-8. Make the report skip them, say why, write every other device, and exit 1. Catch those two problems and nothing else. 30 min
ch 5 Regex and TextFSMThe same table with a regular expression and with ntc-templates, checked against each other. Then a TextFSM template for a command ntc-templates doesn’t cover. 70 min lesson 5
  1. 5.1 One table, two parsersParse show ip interface brief with a regular expression of your own, then with ntc-templates, and make the two give exactly the same list of dicts. 30 min
  2. 5.2 A template of your ownntc-templates has no template for show ntp associations on cisco_ios. Write one in TextFSM, so every server comes back as a dict with its address, reference clock, stratum and reach. 40 min
ch 6 NetmikoLog in to r1 from Python, with the password in .env, not in the code. Then every device, two device types, and a wrong password that gets one clear line instead of a traceback. 70 min lesson 6
  1. 6.1 Log in to r1 from PythonWrite connect() and interface_table() in netlab/devices.py: log in to a device with Netmiko, the username and password read from .env, and get show ip interface brief back as a list of dicts. Then run the script that prints r1’s interfaces. 30 min
  2. 6.2 Every device, and a login that failsWrite a Device dataclass and collect(), which runs one command on r1, r2, sw1 and r3 (two device types) and returns every parsed table. When a login fails or a device doesn’t answer, raise DeviceError with one line that names the device, so scripts/collect.py prints that line instead of a traceback. 40 min
ch 7 YAML, JSON and RESTThe inventory from a YAML file, four values YAML reads differently from you, a JSON round trip, then the same inventory from the lab’s API, with a token. 60 min lesson 7
  1. 7.1 The inventory in YAML and JSONWrite load_inventory(), save_json() and load_json() in netlab/inventory.py: the lab’s devices from inventory.yml, out to a JSON file and back, unchanged. Then find the four values at the top of inventory.yml that YAML reads differently from you, and quote them. 25 min
  2. 7.2 The same inventory from an APIWrite get_devices(), which asks the lab’s API for its devices with the token and turns a 403 or no answer into an ApiError with one line, and api_to_inventory(), which reshapes the API’s nested JSON into the same list of dicts as inventory.yml. 35 min
ch 8 Capstone: an inventory to CSVInventory in, show version out, one CSV row per device. Then a router that’s down and one that doesn’t exist, and two command-line options. 90 min lesson 8
  1. 8.1 Every device, one CSVWrite the three steps scripts/inventory.py runs: load the inventory, turn one device’s show version into a row, and write the rows with csv.DictWriter. Then make the row come out the same for r3, the Pulse router. 40 min
  2. 8.2 A router that's down, a router that isn't thereStop r2 and add a router that was never built, then make run() carry on past both: every device that answered goes in the CSV, the ones that didn’t are listed with a reason, and the script exits 2. 25 min
  3. 8.3 Two options, two sources, one CSVGive scripts/inventory.py a real command line with argparse: –source yaml|api and –out FILE, with help and errors for free. Then show that the YAML file and the API give the same CSV. 25 min