3 回答
TA贡献1788条经验 获得超4个赞
尝试这个 :
import ntc_templates
from ntc_templates.parse import parse_output
vlan_output = (
"VLAN Name Status Ports\n"
"---- -------------------------------- --------- -------------------------------\n"
"1 default active Po1, Eth1/1, Eth1/2, Eth1/3\n"
" Eth1/4, Eth1/5, Eth1/6, Eth1/7\n"
" Eth1/8, Eth1/9, Eth1/10, Eth1/11\n"
" Eth1/12, Eth1/13, Eth1/14\n"
" Eth1/15, Eth1/16, Eth1/17\n"
" Eth1/18, Eth1/19, Eth1/20\n"
" Eth1/21, Eth1/22, Eth1/23\n"
" Eth1/24, Eth1/25, Eth1/26\n"
" Eth1/27, Eth1/28, Eth1/29\n"
" Eth1/30, Eth1/31, Eth1/32\n"
" Eth1/33, Eth1/34, Eth1/35\n"
" Eth1/36, Eth1/38, Eth1/39\n"
" Eth1/40, Eth1/41, Eth1/42\n"
" Eth1/43, Eth1/44, Eth1/45\n"
" Eth1/46, Eth1/47, Eth1/48\n"
"10 test active Po1, Eth1/37, Eth1/41, Eth1/42\n"
" Eth1/43, Eth1/44, Eth1/45\n"
" Eth1/46, Eth1/47, Eth1/48\n")
vlan_parsed = parse_output(platform="cisco_ios", command="show vlan", data=vlan_output)
print(vlan_parsed)
它将返回一个这样的字典:
[{'vlan_id': '1', 'name': 'default', 'status': 'active', 'interfaces': ['Po1', 'Eth1/1', 'Eth1/2', 'Eth1/3', 'Eth1/4', 'Eth1/5', 'Eth1/6', 'Eth1/7', 'Eth1/8', 'Eth1/9', 'Eth1/10', 'Eth1/11', 'Eth1/12', 'Eth1/13', 'Eth1/14', 'Eth1/15', 'Eth1/16', 'Eth1/17', 'Eth1/18', 'Eth1/19', 'Eth1/20', 'Eth1/21', 'Eth1/22', 'Eth1/23', 'Eth1/24', 'Eth1/25', 'Eth1/26', 'Eth1/27', 'Eth1/28', 'Eth1/29', 'Eth1/30', 'Eth1/31', 'Eth1/32', 'Eth1/33', 'Eth1/34', 'Eth1/35', 'Eth1/36', 'Eth1/38', 'Eth1/39', 'Eth1/40', 'Eth1/41', 'Eth1/42', 'Eth1/43', 'Eth1/44', 'Eth1/45', 'Eth1/46', 'Eth1/47', 'Eth1/48']}, {'vlan_id': '10', 'name': 'test', 'status': 'active', 'interfaces': ['Po1', 'Eth1/37', 'Eth1/41', 'Eth1/42', 'Eth1/43', 'Eth1/44', 'Eth1/45', 'Eth1/46', 'Eth1/47', 'Eth1/48']}]
安装 ntc_templates 时要小心(例如使用 pip install ntc_templates)。
它将使用已经可以安装的 textfsm 模块。如果你遇到:
ModuleNotFoundError: No module named 'clitable'
赶紧跑 :
pip install --upgrade textfsm==0.4.1
并再次运行脚本
TA贡献1829条经验 获得超7个赞
在数据中的值之间添加逗号,从而制作一个 csv 文件。然后按照以下关于将 csv(和 excel)转换为 JSON 的 Web 文章顶部的说明进行操作。您所需的输出是 JSON 格式。
https://medium.com/coinmonks/parsing-a-spreadsheet-into-a-json-file-using-python-6118f5c70bd3
从与开始
导入csv、json
TA贡献1785条经验 获得超4个赞
它涉及很多解析。我希望下面的代码片段有助于这样做
s = """VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
1 default active Po1, Eth1/1, Eth1/2, Eth1/3
Eth1/4, Eth1/5, Eth1/6, Eth1/7
Eth1/8, Eth1/9, Eth1/10, Eth1/11
Eth1/12, Eth1/13, Eth1/14
Eth1/15, Eth1/16, Eth1/17
Eth1/18, Eth1/19, Eth1/20
Eth1/21, Eth1/22, Eth1/23
Eth1/24, Eth1/25, Eth1/26
Eth1/27, Eth1/28, Eth1/29
Eth1/30, Eth1/31, Eth1/32
Eth1/33, Eth1/34, Eth1/35
Eth1/36, Eth1/38, Eth1/39
Eth1/40, Eth1/41, Eth1/42
Eth1/43, Eth1/44, Eth1/45
Eth1/46, Eth1/47, Eth1/48
10 test active Po1, Eth1/37, Eth1/41, Eth1/42
Eth1/43, Eth1/44, Eth1/45
Eth1/46, Eth1/47, Eth1/48"""
new_line_split = s.split("\n")
headers = list(filter(None, new_line_split[0].split(" ")))
print(headers)
sessions_data = []
ports = []
session = {}
for line in new_line_split[2:]:
values = list(filter(None,line.split(" ")))
for index, value in enumerate(values):
if value.startswith("Eth", 0) or value.startswith("Po", 0):
ports.append(value.strip(","))
session[headers[3]] = ports
elif index in [0,1,2]:
if index == 0:
session = {}
ports = []
elif bool(session):
sessions_data.append(session)
session[headers[index]] = value
print(sessions_data)
添加回答
举报