我正在尝试使用 TextFSM 从 Cisco 路由器解析控制台 (con/aux/vty) 的配置,特别是命令 show running-config | 的输出。部分线。出现此问题的原因是,这部分配置并未像往常一样以“!”结束“line vtys”,这会在尝试检测记录的结尾而不错过该行作为下一条记录的开头时出现问题。
我期望以结构化格式提取以下数据:
"LINE_TYPE": con/aux/vty
"LINE_NUM": number
"ACCESS_CLASS": number
"EXEC_TIMEOUT": number
"LOGIN_AUTHENTICATION": string
"STOPBITS": number
命令输出示例:
line con 0
exec-timeout 0 0
login authentication autCON
stopbits 1
!
line aux 0
stopbits 1
login authentication autAUX
line vty 0 4
exec-timeout 1 0
login authentication autAAA
line vty 5 15
exec-timeout 2 0
login authentication autBBB
password yourmom
line vty 16
access-class 55 in vrf-also
exec-timeout 3 0
login authentication autCCC
!
我测试了以下模板(在数百个其他试验中!),但它未对齐。这可以在我最后附加的结果的 LINE_TYPE 和 LOGIN_AUTHENTICATION 行中观察到:
TextFSM File
Value Required LINE_TYPE (\S+)
Value LINE_NUM (\S.*)
Value EXEC_TIMEOUT (\d+ \d+|\S+)
Value STOPBITS (\d+)
Value TRANSPORT (\S+)
Value AUTHORIZATION (\S+)
Value ACCOUNTING_CONNECTION (\S.*)
Value ACCOUNTING_EXEC (\S+)
Value LOGIN_AUTHENTICATION (\S+)
Value HISTORY_SIZE (\d+)
Value ACCESS_CLASS (\S+.*)
Start
^line ${LINE_TYPE} ${LINE_NUM} -> Continue.Record
^\s*exec-timeout ${EXEC_TIMEOUT}*
^\s*stopbits ${STOPBITS}*
^\s*transport input ${TRANSPORT}*
^\s*authorization exec ${AUTHORIZATION}*
^\s*accounting connection ${ACCOUNTING_CONNECTION}*
^\s*accounting exec ${ACCOUNTING_EXEC}*
^\s*login authentication ${LOGIN_AUTHENTICATION}*
^\s*history size ${HISTORY_SIZE}*
^\s*access-class ${ACCESS_CLASS}*
^\Z -> Record
Result
[
{
"ACCESS_CLASS": "",
"ACCOUNTING_CONNECTION": "",
"ACCOUNTING_EXEC": "",
"AUTHORIZATION": "",
"EXEC_TIMEOUT": "",
"HISTORY_SIZE": "",
"LINE_NUM": "0",
"LINE_TYPE": "con",
"LOGIN_AUTHENTICATION": "",
"STOPBITS": "",
"TRANSPORT": ""
},
{
"ACCESS_CLASS": "",
"ACCOUNTING_CONNECTION": "",
"ACCOUNTING_EXEC": "",
"AUTHORIZATION": "",
"EXEC_TIMEOUT": "0 0",
"HISTORY_SIZE": "",
"LINE_NUM": "0",
"LINE_TYPE": "aux",
"LOGIN_AUTHENTICATION": "autCON",
"STOPBITS": "1",
"TRANSPORT": ""
},
{
"ACCESS_CLASS": "",
"ACCOUNTING_CONNECTION": "",
"ACCOUNTING_EXEC": "",
"AUTHORIZATION": "",
"EXEC_TIMEOUT": "",
"HISTORY_SIZE": "",
"LINE_NUM": "0 4",
"LINE_TYPE": "vty",
"LOGIN_AUTHENTICATION": "autAUX",
"STOPBITS": "1",
"TRANSPORT": ""
},
{
"ACCESS_CLASS": "",
"ACCOUNTING_CONNECTION": "",
"ACCOUNTING_EXEC": "",
"AUTHORIZATION": "",
"EXEC_TIMEOUT": "1 0",
"HISTORY_SIZE": "",
"LINE_NUM": "5 15",
"LINE_TYPE": "vty",
"LOGIN_AUTHENTICATION": "autAAA",
"STOPBITS": "",
"TRANSPORT": ""
},
{
"ACCESS_CLASS": "",
"ACCOUNTING_CONNECTION": "",
"ACCOUNTING_EXEC": "",
"AUTHORIZATION": "",
"EXEC_TIMEOUT": "2 0",
"HISTORY_SIZE": "",
"LINE_NUM": "16",
"LINE_TYPE": "vty",
"LOGIN_AUTHENTICATION": "autBBB",
"STOPBITS": "",
"TRANSPORT": ""
}
]
问题在于,TextFSM 默认情况下会逐行解析,而你的配置行并非总是在同一个记录块中。 因此,你需要调整模板以更好地处理跨越多行的配置块。
以下是一种改进后的 TextFSM 模板,可以正确解析你的配置:
Value Required LINE_TYPE (\S+)
Value LINE_NUM (\S.*)
Value EXEC_TIMEOUT (\d+ \d+|\S+)
Value STOPBITS (\d+)
Value LOGIN_AUTHENTICATION (\S+)
Value ACCESS_CLASS (\S+.*)
Start
^line ${LINE_TYPE} ${LINE_NUM} -> Record Start
Record Start
^\s*exec-timeout ${EXEC_TIMEOUT}*
^\s*stopbits ${STOPBITS}*
^\s*login authentication ${LOGIN_AUTHENTICATION}*
^\s*access-class ${ACCESS_CLASS}*
^line|^\Z -> Record Stop
Record Stop
改进说明:
-
Record Start
和Record Stop
状态: 引入了新的状态来定义每个记录的开始和结束。 -
Record Start
状态会在匹配到line ...
行后激活,并开始收集该记录的数据。 -
Record Stop
状态会在遇到新的line ...
行(表示新记录开始)或到达文件末尾 (\Z
) 时激活,并将收集到的数据保存为一条记录。 - 正则表达式调整: 部分正则表达式进行了微调,以更好地匹配数据并提高解析的准确性。
使用此模板,你应该能够正确解析 Cisco 路由器的控制台配置,即使配置行跨越多行。
标签:python,parsing,cisco,python-textfsm,ciscoconfparse From: 77363853