splitlines, split() & parsing tables into dictionaries
Turn show output into data
Lesson 5 of 20 · 14 min · Module 2: Read the network
Show commands print tables for people. A few lines of Python turn them into data a script can check.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Split command output into lines and words
- Handle a column that contains spaces
- Turn each line into a dictionary
The idea
splitlines() gives one string per line, and split() without an argument splits a line on any run of spaces. In show ip interface brief the first word is the interface, the second the address and the last the protocol. The status column can contain a space, as in administratively down, so join the words between the method and the protocol: " ".join(parts[4:-1]). Skip the header line, and turn unassigned into None so the data means what it says.
line = "Vlan1 unassigned YES unset administratively down down"
parts = line.split()
print(parts[0], parts[-1], " ".join(parts[4:-1]))
01split() and split(" ") differ
split() treats any run of spaces as one separator. split(" ") cuts at every single space and leaves empty strings behind.
line = "Vlan99 10.0.0.11 YES NVRAM up up"
print(line.split())
print(line.split(" ")[:4])['Vlan99', '10.0.0.11', 'YES', 'NVRAM', 'up', 'up']
['Vlan99', '', '', '10.0.0.11']02A status with a space
Counting from both ends keeps the columns right: the protocol is always the last word, and the status is everything between the method and the protocol.
parts = "Vlan1 unassigned YES unset administratively down down".split()
print(parts[-1])
print(" ".join(parts[4:-1]))down
administratively down03Skip the header
The first line holds the column names. [1:] starts the loop at the second line.
import os
from netmiko import ConnectHandler
USERNAME = os.environ["NET_USERNAME"]
PASSWORD = os.environ["NET_PASSWORD"]
device = {"device_type": "cisco_ios", "host": "10.0.0.11",
"username": USERNAME, "password": PASSWORD}
with ConnectHandler(**device) as conn:
lines = conn.send_command("show ip interface brief").splitlines()
print(lines[0])
print(lines[1].split()[0])Interface IP-Address OK? Method Status Protocol
Vlan1Your turn
parse_interfaces(output) for the text of show ip interface brief. It returns a list of dictionaries with the keys name, ip, status and protocol, where ip is None for unassigned interfaces. Use it on sw1 and build down: the names of the interfaces whose status is exactly down (not administratively down), in order.What the checks look for
- parse_interfaces reads name, ip, status and protocol
- parse_interfaces reads every interface of a real switch
- down lists the interfaces that are down on sw1
- Read the interfaces from sw1
Take this with you
Parse each line into a dictionary once; checks and reports then work on data, not on text.
Go a little further
Also count the interfaces that are up/up on sw1 and print the number.