TextFSM parsing column-aligned CLI output where optional columns shift due to variable whitespace

3 weeks ago 29
ARTICLE AD BOX

I'm trying to parse output where the records are aligned via variable white space. Here is the output in question:

VLAN Table VLAN VLAN Name Type Secure eth0 eth1 eth2 eth3 eth4 ---- --------- ---- ------ ---- ---- ---- ---- ---- 100 Port_1 Bridged Disable T U 200 Port_2 Bridged Disable T U 300 Port_3 Bridged Disable T U 400 Port_4 Bridged Disable T U 1249 AP_Mgmt Bridged Disable T

Specifically, the eth{0-4} columns. I'd like to match either T, U, or a single \s white space per each eth{n} column. So the desired output would look something like this:

[['100', 'Port_1', 'Bridged', 'T', 'U ', ' ', ' ', ''], ['200', 'Port_2', 'Bridged', 'T', ' ', 'U', ' ', ' '], ['300', 'Port_3', 'Bridged', 'T', ' ', ' ', 'U', ' '], ['400', 'Port_4', 'Bridged', 'T', ' ', ' ', ' ', 'U'], ['1249', 'AP_Mgmt', 'Bridged', 'T', ' ', ' ', ' ', ' ']]

Also worth noting that I plan on doing some post processing to normalize this data into Python dataclasses or BaseModels. Just need the proper tagging status to match the correct port columns.

I've tried making the eth{0-4} columns their own TextFSM Required values with the follow regexp: ([TU\s]) and trying to match that value on every 7th whitespace between the eth columns as seen in the template here:

Value Required vid (\d+) Value Required vlan_name (\S+) Value Required type (\S+) Value Required eth0 ([TU\s]) Value Required eth1 ([TU\s]) Value Required eth2 ([TU\s]) Value Required eth3 ([TU\s]) Value Required eth4 ([TU\s]) Start ^\s*${vid}\s+${vlan_name}\s+${type}\s+\S+\s+${eth0}\s+${eth1}\s{7,}${eth2}\s{7,}${eth3}\s{7,}${eth4} -> Record

Current output:

[['400', 'Port_4', 'Bridged', 'T', ' ', ' ', ' ', 'U']]
Read Entire Article