I am trying to create a textfsm template for command of a Cisco switch. the command is show platform
. This command doesn't have a predefined template in ntc-templates
. The CLI command output (Serials and MAC addresses are fake):
Switch Ports Model Serial No. MAC address Hw Ver. Sw Ver.
------ ----- --------- ----------- -------------- ------- --------
1 32 C9200-24P 2FCYCZBVY4R df68.ebfc.44bb V01 17.03.03
2 32 C9200-24P PW73B4U6UVW 982a.7043.0b7f V01 17.03.03
3 32 C9200-24P PRJ5QKQE73S 3b9f.390b.04d2 V01 17.03.03
Switch/Stack Mac Address : df68.ebfc.44bb - Local Mac Address
Mac persistency wait time: Indefinite
Current
Switch# Role Priority State
-------------------------------------------
*1 Active 15 Ready
2 Standby 14 Ready
3 Member 13 Ready
My Python try is as follows:
import textfsm
plat = """
Switch Ports Model Serial No. MAC address Hw Ver. Sw Ver.
------ ----- --------- ----------- -------------- ------- --------
1 32 C9200-24P 2FCYCZBVY4R df68.ebfc.44bb V01 17.03.03
2 32 C9200-24P PW73B4U6UVW 982a.7043.0b7f V01 17.03.03
3 32 C9200-24P PRJ5QKQE73S 3b9f.390b.04d2 V01 17.03.03
Switch/Stack Mac Address : df68.ebfc.44bb - Local Mac Address
Mac persistency wait time: Indefinite
Current
Switch# Role Priority State
-------------------------------------------
*1 Active 15 Ready
2 Standby 14 Ready
3 Member 13 Ready
"""
with open("plat.textfsm") as template:
fsm = textfsm.TextFSM(template)
result = fsm.ParseText(plat)
print(fsm.header)
print(result)
and the plat.textfsm
template file
Value SWITCH ([1-8])
Value PORTS (\d+)
Value MODEL (\S+|\S+\d\S+)
Value SERIAL (\S+)
Value MAC ([0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4})
Value HARDWARE (\S+)
Value VERSION (\S+)
Value ROLE (Active|Standby|Member)
Value PRIORITY ([1-9]|1[0-5])
Value STATE (\S+)
Start
^.${SWITCH}\s+${PORTS}\s+${MODEL}\s+${SERIAL}\s+${MAC}\s+${HARDWARE}\s+${VERSION} -> Stack
Stack
^.*${ROLE}\s+${PRIORITY}\s+${STATE} -> Record
The output I get so far is:
['SWITCH', 'PORTS', 'MODEL', 'SERIAL', 'MAC', 'HARDWARE', 'VERSION', 'ROLE', 'PRIORITY', 'STATE']
[['1', '32', 'C9200-24P', '2FCYCZBVY4R', 'df68.ebfc.44bb', 'V01', '17.03.03', 'Active', '15', 'Ready'],
['', '', '', '', '', '', '', 'Standby', '14', 'Ready'],
['', '', '', '', '', '', '', 'Member', '13', 'Ready']]
I want to add Role
, Priority
, Current State
to each list in the output like:
['SWITCH', 'PORTS', 'MODEL', 'SERIAL', 'MAC', 'HARDWARE', 'VERSION', 'ROLE', 'PRIORITY', 'STATE']
[['1', '32', 'C9200-24P', '2FCYCZBVY4R', 'df68.ebfc.44bb', 'V01', '17.03.03', 'Active', '15', 'Ready'],
['2', '32', 'C9200-24P', 'PW73B4U6UVW', '982a.7043.0b7f', 'V01', '17.03.03', 'Standby', '14', 'Ready'],
['3', '32', 'C9200-24P', 'PRJ5QKQE73S', '3b9f.390b.04d2', 'V01', '17.03.03', 'Member', '13', 'Ready']]
How can I edit the textfsm template to do this using the provided command output? What am I missing to get the correct output?