Per-vendor commands and parsers, dispatch tables & set operations

Audit VLANs on two vendors

Lesson 8 of 20 · 15 min · Module 2: Read the network

Cisco and Huawei print the same facts in different ways. Keep one parser per vendor, pick the right one with a dictionary, and compare the results as data.

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…

  • Pick a command per vendor from a dictionary
  • Parse VLAN tables from Cisco and Huawei
  • Compare sets of VLANs

The idea

A dispatch table is a dictionary whose values are functions or commands: COMMANDS["huawei"] gives display vlan, and PARSERS[platform](output) calls the right parser. On Cisco, VLAN lines in show vlan brief start with the VLAN number; on Huawei, display vlan lists every VLAN twice, in two tables. Parse both into sets of integers, then compare with set operations: | (in any), & (in all) and - (in the first but not the second).

COMMANDS = {"cisco_ios": "show vlan brief", "huawei": "display vlan"}
print(COMMANDS["huawei"])
print({1, 10, 30} - {1, 10})

01Cisco lines start with the VLAN

Ports that do not fit on one line continue on the next one, which starts with spaces. Checking the first word with isdigit() keeps only the VLAN lines.

import os
from netmiko import ConnectHandler

USERNAME = os.environ["NET_USERNAME"]
PASSWORD = os.environ["NET_PASSWORD"]

with ConnectHandler(device_type="cisco_ios", host="10.0.0.11",
                    username=USERNAME, password=PASSWORD) as conn:
    lines = conn.send_command("show vlan brief").splitlines()
print("\n".join(lines[:6]))
print(sorted(int(l.split()[0]) for l in lines
             if l.strip() and l.split()[0].isdigit()))
Output

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Gi0/7, Gi0/8, Gi0/10
10   USERS                            active    Gi0/1, Gi0/2, Gi0/3, Gi0/5,
                                                Gi0/6
[1, 10, 20, 40, 99, 1002, 1003, 1004, 1005]

02Huawei lists each VLAN twice

display vlan has a ports table and a status table. A set keeps each VLAN once, however often it appears.

with ConnectHandler(device_type="huawei", host="10.0.0.13",
                    username=USERNAME, password=PASSWORD) as conn:
    text = conn.send_command("display vlan")
numbers = [int(l.split()[0]) for l in text.splitlines()
           if l.strip() and l.split()[0].isdigit()]
print(numbers)
print(sorted(set(numbers)))
Output
[1, 10, 20, 30, 99, 1, 10, 20, 30, 99]
[1, 10, 20, 30, 99]

03In any, in all, the difference

set.union(*groups) and set.intersection(*groups) work on any number of sets. sorted() turns a set into an ordered list for printing.

groups = [{1, 10, 40}, {1, 10}, {1, 10, 30}]
print(sorted(set.union(*groups)))
print(sorted(set.intersection(*groups)))
print(sorted(set.union(*groups) - set.intersection(*groups)))
Output
[1, 10, 30, 40]
[1, 10]
[30, 40]

Your turn

Collect the VLAN IDs of sw1, sw2 and sw3 into vlans, a dictionary of sets keyed by switch name (show vlan brief on Cisco, display vlan on Huawei). Then build missing: the set of VLAN IDs that exist on at least one of the three switches but not on all of them. Leave out Cisco's built-in VLANs 1002 to 1005.

What the checks look for

  • vlans has a set of VLAN IDs for each switch
  • sw3's Huawei VLANs are parsed
  • Cisco VLANs are parsed
  • missing is {30, 40}
  • Used the vendor command on each switch
Try it in the editor →

Take this with you

One command and one parser per vendor, chosen from a dictionary; compare the parsed data, never the raw text.

Go a little further

For each VLAN in missing, also print which switches do not have it.

Learn Network Automation for free

20 short lessons with real code and instant checks.

See the whole course →