Case File: The Interface Report
Every night someone reads the interface table of forty routers by eye. Teach Python to read one saved 'show ip interface brief' for you: parse the lines, handle the two-word status that breaks a naive split, count what is healthy, list what is shut down on purpose, and find the real faults. The table is generated for you alone.
Sabi Logistics · Automation 1
The Interface Report
Every morning a person reads the interface tables of forty routers by eye, and last week one broken port was missed
Tunde Night NOC lead · Phone call
Tunde. Every night I log in to our routers one by one, run show ip interface brief, and read the table with my eyes. Forty routers, thirty lines each. Last week I missed a port that had been down for two days, because I was tired and it looked like all the others.
I do not need anything clever. I need a script that reads the saved output and tells me four things: how many interfaces there are, how many are really healthy, which ones somebody shut down on purpose (I do not want to be told about those), and which ones are actually broken. Broken means: it has an IP address, nobody shut it down, and it is not up and up.
I saved one router’s output to a file. Show me you can read it with Python, and then we will do the other thirty-nine.
The brief
What you need to know first.
- Network devices print text for people. To automate, you turn that text into data (lists and dictionaries) and then ask questions of the data. This is the most common first step in network automation, and you can do it with plain Python and no libraries.
- The table.
show ip interface briefprints one line per interface: name, IP address, OK?, method, status, protocol. The status and the protocol are different things: status is the physical/administrative state of the port, protocol is whether the line protocol works (it needs a signal from the other end). A healthy interface is up/up. administratively down / down means somebody ranshutdownon the port. down/down with an address is usually a cable or the device at the other end. up/down is a layer 2 problem (a mismatch, a keepalive failure). - Reading a line.
line.split()cuts a line at every run of spaces and gives a list of words. Python lists count from the front (t[0]) and from the back (t[-1]). The name ist[0], the address ist[1], the protocol ist[-1]. The status is everything between the method (t[3]) and the protocol:" ".join(t[4:-1]). That slice is the whole trick, because “administratively down” is two words. - Collect rows as dictionaries:
rows.append({"name": t[0], "ip": t[1], "status": ..., "proto": t[-1]}). After that, every question is a short loop or a comprehension:[r["name"] for r in rows if r["status"] == "administratively down"],sum(1 for r in rows if ...). - Combine conditions carefully:
a and b and not (c and d). Write the condition as a sentence first (“has an address, is not shut down, is not up/up”) and then translate it. - Skip the header:
splitlines()[1:]. Count what you parse (len(rows)) and compare it with the number of lines in the file: if they differ, you lost a line. - Write the answers into the variables in the starter (
n_interfaces,up_up,admin_down,broken,who_has). The checker reads those variables after your code runs. Lists must be in the order the interfaces appear in the file.
The lab gives you interfaces.txt, generated for you alone. It runs real Python in your browser (about 10 MB the first time, then remembered).
Preparing your data…
On a real system
The lab modelled the rules with a script. These are the commands that you would type on the real thing.
| You want to | Type | What you get |
|---|---|---|
| list the interfaces of a Cisco router with their addresses and state | show ip interface brief | one line per interface: address, status and protocol |
| read the error counters of one interface | show interfaces GigabitEthernet0/1 | input and output errors, CRC and drops since the last reset |
| list the interfaces on MikroTik RouterOS | /interface print | name, type, MTU and whether each interface is running |
Only run commands on systems that you own or have written permission to test.
Tunde: “It found my three broken ports in a tenth of a second. Now my problem is the other thirty-nine routers. My routers are not all the same model. If the text looks a bit different on the old ones, how do I know the script is not quietly giving me wrong answers?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Tunde: “So the script counts lines and I compare. That is a check I can actually do at four in the morning.”
What a senior engineer would do next. Keep the raw output next to the report, so any number can be checked against the evidence. Add assertions that fail loudly: every parsed line has at least five words, the number of rows equals the number of lines, no interface name appears twice. Run the parser on saved samples from every model before trusting it on all of them. When the device offers structured output (a REST API, NETCONF, or JSON from show ... | json), prefer it to parsing text. When it does not, libraries such as TextFSM with ready-made templates parse the common commands, but you still need to understand what they do, which is what you just did by hand. Finally, report the healthy count as well as the faults: a report that says “0 problems” because it read nothing looks exactly like a report that read everything.
The trap. Splitting by column position, or taking the status as one word. Both work on the sample you tried and fail on the first port that was shut down on purpose.
Write the note for the night shift
Write the short note that goes with the script: what it reads and what it reports, how it tells a port that was shut down on purpose from a real fault, why splitting on spaces needs care for the status column, how you check that it parsed every line, and what you would try before trusting it on routers of another model.
🧪 Want to practise this on your own computer? See how to set up your lab →