首页 > 解决方案 > 如何在 textfsm 中捕获多行但在同一行打印

问题描述

我有以下数据

SW#show syslog-servers

IP/IPv6 Address/Hostname    Port    Severity        Description
--------------------------  ------  --------------  --------------
1.1.1.1                     514     informational
2.2.2.2                     515     warning
3.3.3.3                     516     informational

Transport Type Authentication    Certificate Index
-------------- ----------------- -------------------
UDP
UDP
TCP

switch show 命令的图像

Value IP (\S+)
Value PORT (\d+)
Value SEVERITY (\S+)
Value TRANSPORT (TCP|UDP)

Start
 ^.*---- -> syslog

syslog
 ^${IP}\s+${PORT}\s+${SEVERITY} -> Record
 ^${TRANSPORT} -> Record

我希望输出看起来像这样

[
 {
  "IP": "1.1.1.1",
  "PORT": "514",
  "SEVERITY": "informational",
  "TRANSPORT": "UDP"
 },
 {
  "IP": "2.2.2.2",
  "PORT": "515",
  "SEVERITY": "warning",
  "TRANSPORT": "UDP"
 },
 {
  "IP": "3.3.3.3",
  "PORT": "516",
  "SEVERITY": "informational",
  "TRANSPORT": "TCP"
 }
]

但相反,我得到了这个

[
 {
  "IP": "1.1.1.1",
  "PORT": "514",
  "SEVERITY": "informational",
  "TRANSPORT": ""
 },
 {
  "IP": "2.2.2.2",
  "PORT": "515",
  "SEVERITY": "warning",
  "TRANSPORT": ""
 },
 {
  "IP": "3.3.3.3",
  "PORT": "516",
  "SEVERITY": "informational",
  "TRANSPORT": ""
 },
 {
  "IP": "",
  "PORT": "",
  "SEVERITY": "",
  "TRANSPORT": "UDP"
 },
 {
  "IP": "",
  "PORT": "",
  "SEVERITY": "",
  "TRANSPORT": "UDP"
 },
 {
  "IP": "",
  "PORT": "",
  "SEVERITY": "",
  "TRANSPORT": "TCP"
 }
]

标签: pythonpython-textfsm

解决方案


推荐阅读