0

我正在尝试为 Cisco 交换机的命令创建一个 textfsm 模板。命令是show platform。此命令在ntc-templates. CLI 命令输出(序列号和 MAC 地址是假的)

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

我的 Python 尝试如下:

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)

plat.textfsm模板文件

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

到目前为止我得到的输出是:

['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']]

我想将Role, Priority,添加Current State到输出中的每个列表中,例如:

['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']]

如何使用提供的命令输出编辑 textfsm 模板以执行此操作?我缺少什么来获得正确的输出?

4

0 回答 0