调整Textfsm模板以完整解析Nokia 7750 VPLS配置的所有mesh-sdp
解决Nokia 7750 VPLS配置中Mesh-SDP条目不全的Textfsm模板问题
配置片段示例
vpls 100 customer 10 create description "Customer A VPLS" mesh-sdp 1:100 create mesh-sdp 1:101 create mesh-sdp 2:200 create vpls 200 customer 20 create description "Customer B VPLS" mesh-sdp 3:300 create
预期解析结果
| VPLS_ID | CUSTOMER_ID | MESH_SDP |
|---|---|---|
| 100 | 10 | 1:100 |
| 100 | 10 | 1:101 |
| 100 | 10 | 2:200 |
| 200 | 20 | 3:300 |
现有问题模板
Value VPLS_ID (\d+) Value CUSTOMER_ID (\d+) Value MESH_SDP (\S+) Start ^vpls ${VPLS_ID} customer ${CUSTOMER_ID} create -> Record ^\s+mesh-sdp ${MESH_SDP} create -> Record
问题分析
现有模板匹配到vpls ... create或mesh-sdp ... create就立即执行Record,导致每个VPLS实例的第一个mesh-sdp会和VPLS基础信息分开记录,后续的mesh-sdp无法关联到对应的VPLS和客户ID,最终只能保留第一个条目。
修正后的Textfsm模板
Value VPLS_ID (\d+) Value CUSTOMER_ID (\d+) Value MESH_SDP (\S+) Start ^vpls ${VPLS_ID} customer ${CUSTOMER_ID} create -> VplsSection VplsSection ^\s+mesh-sdp ${MESH_SDP} create -> Record ^vpls ${VPLS_ID} customer ${CUSTOMER_ID} create -> Clear; VplsSection ^exit -> Start
模板逻辑说明
- 新增
VplsSection状态:进入该状态后,会持续保留当前匹配到的VPLS_ID和CUSTOMER_ID上下文 - 每匹配到一个
mesh-sdp就执行Record,自动关联当前状态下的VPLS和客户信息 - 匹配到新的
vpls条目时,执行Clear清空旧变量,切换到新的VplsSection状态 - 遇到配置中的
exit层级退出命令时,回到Start状态等待下一个VPLS实例
验证代码
import textfsm from io import StringIO # 待解析配置 config = """vpls 100 customer 10 create description "Customer A VPLS" mesh-sdp 1:100 create mesh-sdp 1:101 create mesh-sdp 2:200 create vpls 200 customer 20 create description "Customer B VPLS" mesh-sdp 3:300 create """ # 修正后的模板内容 template_content = """Value VPLS_ID (\d+) Value CUSTOMER_ID (\d+) Value MESH_SDP (\S+) Start ^vpls ${VPLS_ID} customer ${CUSTOMER_ID} create -> VplsSection VplsSection ^\s+mesh-sdp ${MESH_SDP} create -> Record ^vpls ${VPLS_ID} customer ${CUSTOMER_ID} create -> Clear; VplsSection ^exit -> Start """ # 执行解析 template = textfsm.TextFSM(StringIO(template_content)) result = template.ParseText(config) # 输出结果 print("解析结果:") print(template.header) for row in result: print(row)
运行代码后,会输出所有关联正确的VPLS与Mesh-SDP条目,与预期结果一致。
内容的提问来源于stack exchange,提问作者finopat
相关产品推荐
相关产品推荐

