online posts 16
python 101 for network engineers

task 2.2 · chapter 2 of 8: strings, lists and dicts · 30 min

A whole show ip interface brief

objective

Turn 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.

you edit, in the labnetlab/parse.py

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
The outputSAMPLE in netlab/parse.py: r1’s real show ip interface brief
Your functionnetlab/parse.py: interfaces_to_dicts() at TODO 2.2
Each dict's keysinterface, ip_address, ok, method, status, protocol
new to this? step by stepbeginner
  1. python, then from netlab.parse import SAMPLE and print(SAMPLE).
  2. Split each line with split() and count the words. Which line has seven?
  3. Challenge: write interfaces_to_dicts() under TODO 2.2: one dict per interface line.
  4. make check T=2.2. If it fails, read the E lines: we expected against and got.

more in meet the lab

1 explain

Columns are a picture, not data

What split() sees

show ip interface brief lines its columns up with spaces so a person can read it. split(), with nothing in the parentheses, cuts a line wherever there are spaces, however many. That works for every column but one: Status. An interface that someone has shutdown shows administratively down, which is two words, so that line splits into seven pieces instead of six.

The way out is to count from both ends. The first four columns (interface, IP address, OK?, method) and the last one (protocol) are always one word each. Whatever is in between is the status.

in one sentence Take the first four words and the last word by position; the status is everything left in the middle.

A loop, a first look

This task needs one thing from lesson 3, a loop: the same lines run once for every item of a list.

for line in text.splitlines():
    words = line.split()
    ...

text.splitlines() is a list of the lines; for line in ... takes each in turn and runs the indented lines under it. Inside a loop, continue skips to the next line, and list.append(x) adds x to the end of a list. Lesson 3 covers loops properly.

Two lines that aren’t interfaces

The header (Interface IP-Address ...) isn’t an interface. A blank line isn’t either, and "".split() is an empty list, [], so asking for its first word fails. Skip both.

No address is None

An interface without an address says unassigned. That’s a word the CLI prints, not an address. In the dicts, write it as None, Python’s value for nothing, so that later code can ask ip_address is None and never has to know which word one OS uses.

in one sentence Translate CLI words into Python values once, where you parse. Everything after that works with clean data.

2 show

Text in, a list of dicts out

r1’s output, the lab’s SAMPLE:

Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet0       172.80.111.11   YES DHCP   up                    up
GigabitEthernet1       192.168.10.1    YES manual up                    up
GigabitEthernet2       10.0.12.1       YES manual up                    up
GigabitEthernet3       10.0.13.1       YES manual up                    up
GigabitEthernet4       unassigned      YES unset  administratively down down
Loopback0              10.255.0.1      YES manual up                    up

What interfaces_to_dicts(SAMPLE) returns when it’s done: six dicts, one per interface. The first and the fifth:

>>> rows = interfaces_to_dicts(SAMPLE)
>>> len(rows)
6
>>> rows[0]
{'interface': 'GigabitEthernet0', 'ip_address': '172.80.111.11', 'ok': 'YES', 'method': 'DHCP', 'status': 'up', 'protocol': 'up'}
>>> rows[4]
{'interface': 'GigabitEthernet4', 'ip_address': None, 'ok': 'YES', 'method': 'unset', 'status': 'administratively down', 'protocol': 'down'}

And with no output at all, no interfaces:

>>> interfaces_to_dicts("")
[]

The check also makes up outputs of its own, 30 of them, with sub-interfaces, VLAN interfaces, interfaces up with the protocol down, and blank lines in odd places.

3 follow along

Count the words

  1. Start the REPL in the repo’s folder and look at the output as Python sees it:

    run in the terminal
    python
    
    >>> from netlab.parse import SAMPLE
    >>> lines = SAMPLE.splitlines()
    >>> len(lines)
    7
    >>> lines[0].split()
    ['Interface', 'IP-Address', 'OK?', 'Method', 'Status', 'Protocol']
    

    Seven lines: the header and six interfaces. The header splits like any other line, so you’ll need to tell it apart. Its first word is a good clue.

  2. Count the words on every line, with a loop:

    >>> for line in lines:
    ...     print(len(line.split()), line.split()[0])
    ...
    6 Interface
    6 GigabitEthernet0
    6 GigabitEthernet1
    6 GigabitEthernet2
    6 GigabitEthernet3
    7 GigabitEthernet4
    6 Loopback0
    

    Type the first line, then the indented one (four spaces), then press Enter on an empty line to run it. GigabitEthernet4 is the one with seven.

  3. Look at GigabitEthernet4’s words, and at what a blank line gives:

    >>> gi4 = lines[5].split()
    >>> gi4[-1]
    'down'
    >>> "".split()
    []
    

    [-1] is the last word, the protocol, whatever the line’s length. An empty list has no first word, so "".split()[0] would be an IndexError: a blank line has to be skipped before you look inside it.

in one sentence Positions from the front and from the back are both fixed; only the middle changes length.

4 challenge

interfaces_to_dicts()

Write interfaces_to_dicts(text) under TODO 2.2, in place of the raise NotImplementedError(...) line. It returns a list with one dict per interface line, each with the keys interface, ip_address, ok, method, status and protocol:

  • every value is a string, except that an unassigned address is None;
  • administratively down is one status, not two;
  • the header and blank lines aren’t interfaces;
  • an output with no interface lines gives [].
hint

Start with an empty list, rows = []. Loop over text.splitlines(), split each line, and if the line has no words, or its first word is "Interface", continue. At the end, return rows.

hint

words[:4] is the first four words, words[-1] the last, and words[4:-1] everything between them. On most lines that middle is ['up'] or ['down']; on one it’s ['administratively', 'down']. " ".join(...) turns either back into one string.

hint

If the check says assert 'unassigned' is None, the word went straight into the dict. None if address == "unassigned" else address gives None for that word and the address for anything else.

hint

ValueError: too many values to unpack (expected 6) means a line like a, b, c, d, e, f = line.split() met a line with seven words. not enough values to unpack (expected 6, got 0) is the same line meeting a blank one.

show the solutionnetlab/parse.py

or pull it into your lab: make solve T=2.2, then git diff --staged shows what changed

def interfaces_to_dicts(text):
    rows = []
    for line in text.splitlines():
        words = line.split()
        if not words or words[0] == "Interface":
            continue
        name, address, ok, method = words[:4]
        status = " ".join(words[4:-1])          # "up", "down" or "administratively down"
        rows.append({
            "interface": name,
            "ip_address": None if address == "unassigned" else address,
            "ok": ok,
            "method": method,
            "status": status,
            "protocol": words[-1],
        })
    return rows

not words is true for an empty list, so blank lines and lines of only spaces are skipped before anything looks inside them. The first four words are fixed, so they can be unpacked into four names at once. The status is everything between them and the protocol, joined back up with a space, so one line of code covers up, down and administratively down. This is screen-scraping by hand, and it’s fragile on purpose: in chapter 5 you’ll get the same table from a regular expression and from TextFSM, and compare.

5 check: run it, then prove it

$ make check T=2.2

when it's right, you'll see

task 2.2  checks/test_2_2.py
checks/test_2_2.py::test_r1 PASSED                                       [ 33%]
checks/test_2_2.py::test_unseen_outputs PASSED                           [ 66%]
checks/test_2_2.py::test_no_interfaces PASSED                            [100%]
============================== 3 passed in 0.01s ===============================
✓ task 2.2 done