3 回答

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
嘗試這個(gè) :
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)
它將返回一個(gè)這樣的字典:
[{'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 時(shí)要小心(例如使用 pip install ntc_templates)。
它將使用已經(jīng)可以安裝的 textfsm 模塊。如果你遇到:
ModuleNotFoundError: No module named 'clitable'
趕緊跑 :
pip install --upgrade textfsm==0.4.1
并再次運(yùn)行腳本

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
在數(shù)據(jù)中的值之間添加逗號(hào),從而制作一個(gè) csv 文件。然后按照以下關(guān)于將 csv(和 excel)轉(zhuǎn)換為 JSON 的 Web 文章頂部的說(shuō)明進(jìn)行操作。您所需的輸出是 JSON 格式。
https://medium.com/coinmonks/parsing-a-spreadsheet-into-a-json-file-using-python-6118f5c70bd3
從與開(kāi)始
導(dǎo)入csv、json

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
它涉及很多解析。我希望下面的代碼片段有助于這樣做
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)
添加回答
舉報(bào)