TextFSM解析Cisco Stackwise链路返回重复接口信息的问题求助
解决TextFSM解析Cisco Stackwise链路输出重复数据的问题
问题背景
使用TextFSM解析Cisco路由器Stackwise链路输出信息时,返回的接口详情出现重复,需要修正TextFSM模板。
Stackwise链路输出样本
Stackwise Virtual Link(SVL) Information: ---------------------------------------- Flags: ------ Link Status ----------- U-Up D-Down Protocol Status --------------- S-Suspended P-Pending E-Error T-Timeout R-Ready ----------------------------------------------- Switch SVL Ports Link-Status Protocol-Status ------ --- ----- ----------- --------------- 1 1 TwentyFiveGigE1/0/1 U R TwentyFiveGigE1/0/2 U R TwentyFiveGigE1/0/3 U R 2 1 TwentyFiveGigE2/0/4 U R TwentyFiveGigE2/0/1 U R TwentyFiveGigE2/0/2 U R TwentyFiveGigE2/0/3 U R
错误的TextFSM模板
Value Required,Filldown SWITCH (\d) Value Required,Filldown SVL (\d) Value Required,Filldown PORTS (\S+) Value Required,Filldown LINK_STATUS (\S+) Value Required,Filldown PROTOCOL_STATUS (\S+) Start ^Switch(\s+([A-Za-z]+\s+)+)[A-Za-z]+-([A-Za-z]+(\s+[A-Za-z]+)+)-[A-Za-z]+ -> Continue ^------ --- ----- ----------- --------------- ^${SWITCH}\s+${SVL}.* -> Continue ^(\d+)\s+(\d+)\s+${PORTS}\s+${LINK_STATUS}\s+${PROTOCOL_STATUS}\s+? -> Continue ^.* -> Record EOF
当前错误输出
[['1', '1', 'TwentyFiveGigE1/0/1', 'U', 'R'], ['1', '1', 'TwentyFiveGigE1/0/1', 'U', 'R'], ['1', '1', 'TwentyFiveGigE1/0/1', 'U', 'R'], ['2', '1', 'TwentyFiveGigE2/0/1', 'U', 'R'], ['2', '1', 'TwentyFiveGigE2/0/1', 'U', 'R'], ['2', '1', 'TwentyFiveGigE2/0/1', 'U', 'R']]
修正方案
修正后的TextFSM模板
Value Required,Filldown SWITCH (\d+) Value Required,Filldown SVL (\d+) Value Required PORTS (\S+) Value Required LINK_STATUS (\S+) Value Required PROTOCOL_STATUS (\S+) Start # 跳过表头行 ^Switch\s+SVL\s+Ports\s+Link-Status\s+Protocol-Status -> Continue # 跳过分隔线 ^------\s+---\s+-----\s+-----------\s+--------------- -> Continue # 匹配带Switch和SVL编号的首行接口 ^${SWITCH}\s+${SVL}\s+${PORTS}\s+${LINK_STATUS}\s+${PROTOCOL_STATUS} -> Record # 匹配后续同Switch/SVL的接口行(开头为空格) ^\s+${PORTS}\s+${LINK_STATUS}\s+${PROTOCOL_STATUS} -> Record # 跳过其他无关行 ^.* -> Continue EOF
关键改动说明
- 移除不必要的Filldown:PORTS、LINK_STATUS、PROTOCOL_STATUS不再使用Filldown,因为每个接口的这些属性是独立的,不需要继承上一行的值,避免重复保留第一个接口的信息。
- 精准匹配接口行:
- 明确匹配两种接口行:带Switch和SVL编号的首行,以及开头为空格的后续接口行,每匹配到一行就触发Record,确保每个接口生成一条独立记录。
- 优化表头与分隔线匹配:用更简洁精准的规则跳过表头和分隔线,避免误匹配。
- 调整Record触发时机:仅在匹配到有效接口行时触发Record,无关行直接跳过,避免重复记录。
修正后预期输出
[['1', '1', 'TwentyFiveGigE1/0/1', 'U', 'R'], ['1', '1', 'TwentyFiveGigE1/0/2', 'U', 'R'], ['1', '1', 'TwentyFiveGigE1/0/3', 'U', 'R'], ['2', '1', 'TwentyFiveGigE2/0/4', 'U', 'R'], ['2', '1', 'TwentyFiveGigE2/0/1', 'U', 'R'], ['2', '1', 'TwentyFiveGigE2/0/2', 'U', 'R'], ['2', '1', 'TwentyFiveGigE2/0/3', 'U', 'R']]
内容的提问来源于stack exchange,提问作者vkumar
相关产品推荐
相关产品推荐

