
This Python network automation lab turns a saved switch configuration into a short list of interfaces with no description. You need Python 3 and a text file. No switch login, extra packages, or configuration changes are required. The result is a review list, not a list of broken ports.
1. Create a small switch configuration to test
When a ticket mentions “the printer port,” a description can save a trip through the floor plan. A missing description does not mean the port is in use or that it needs a change. First, build a report that shows which ports someone should check. Save the sample below as switch-lab.txt.
interface GigabitEthernet1/0/1
description Uplink to lab router
!
interface GigabitEthernet1/0/2
switchport access vlan 20
!
interface GigabitEthernet1/0/3
shutdown
!2. Build the Python network automation lab
Save this script as audit_ports.py in the same folder. It looks for top-level interface lines and checks each block for a non-empty description. This example assumes Cisco IOS-style indentation; it is not a parser for every switch vendor. The script uses the standard library’s Path.read_text method to read the local file.
from pathlib import Path
def missing_descriptions(config):
current = None
has_description = False
for raw in config.splitlines():
line = raw.strip()
if not line:
continue
if not raw[0].isspace() or line == "!":
if current is not None and not has_description:
yield current
current = line.split(maxsplit=1)[1] if line.startswith("interface ") else None
has_description = False
elif current is not None and line.startswith("description "):
has_description = True
if current is not None and not has_description:
yield current
config = Path("switch-lab.txt").read_text(encoding="utf-8")
for port in missing_descriptions(config):
print(port)Open a terminal in that folder and run python audit_ports.py (or python3 audit_ports.py if that is how your system starts Python). The output should be:
GigabitEthernet1/0/2
GigabitEthernet1/0/3
3. Check what the report can and cannot tell you
Add description Lab printer under interface GigabitEthernet1/0/2 in the text file and run the script again. Only GigabitEthernet1/0/3 should remain. That last port is shut down in the sample, but the script still lists it. If your team permits unused ports to stay unlabeled, check their status before opening a ticket.
An empty report also needs a sanity check: an empty file or a config in a different format can produce no output. Before relying on a real report, test it on one example with known missing descriptions. If you use an approved export from work, keep it local and remove sensitive details before sharing it.
This lab checks interface labels. To check for missing or duplicate devices instead, try the Python network inventory CSV tutorial. For guided practice with file handling and network automation, explore the PM Networking Python network automation course.
Python network automation lab FAQ
Can I do the lab without a Cisco switch?
Yes. The sample is plain text. The script never connects to a device. You can practice on a laptop with Python 3 and a text editor.
Does a missing description mean the port is faulty?
No. It means this snapshot does not contain a description for that interface. Status, cabling, and ownership need separate checks.
Does the script edit my configuration file?
No. It only reads the file and prints interface names to the terminal. It does not write to the file or send any command to a switch.

0 Comments