给定以下代码:
https://bpaste.net/show/dd44a1fa01dc
from ciscoconfparse import CiscoConfParse
from pprint import pprint
parse = CiscoConfParse("testconfig.txt", syntax="junos")
interfaces = {}
intfs = parse.find_objects_w_parents(r'^interface', r'^\s+ge-')
for intfobj in intfs:
intf_name = intfobj.text.strip()
interfaces.update({'name': intf_name})
descr = intfobj.re_match_iter_typed(r'description\s+"(\S.+?)"$', group=1)
interfaces.update({'description': descr})
mode = intfobj.re_match_iter_typed(r'port-mode\s+(\S+)\s*$', group=1,
all_children=True)
interfaces.update({'mode': mode})
print (interfaces)
使用以下测试数据:
https://bpaste.net/show/df422a96aaae
interfaces {
ge-2/0/0 {
description "site1;;hostname1;ge-16/0/9;;;TRUST;";
unit 0 {
family ethernet-switching {
port-mode trunk;
}
}
}
ge-2/0/2 {
description "site2;;hostname2;ge-16/0/8;;;TRUST;";
unit 0 {
family ethernet-switching {
port-mode trunk;
}
}
}
vstp {
bpdu-block-on-edge;
vlan VLAN_0005 {
interface ge-2/0/0 {
edge;
}
}
vlan VLAN_0015 {
interface ge-2/0/0 {
edge;
}
interface ge-2/0/2 {
edge;
}
}
}
}
我试图理解为什么我的interfaces={}只包含一行:
{'name': 'ge-2/0/2', 'description': 'site2;;hostname2;ge-16/0/8;;;TRUST;', 'mode': 'trunk'}
我希望它包含来自测试数据的两个接口:
{'name': 'ge-2/0/0', 'description': 'site1;;hostname1;ge-16/0/9;;;TRUST;', 'mode': 'trunk'}
{'name': 'ge-2/0/2', 'description': 'site2;;hostname2;ge-16/0/8;;;TRUST;', 'mode': 'trunk'}
我可以试试这样的
打印(所有接口)
您正在创建dict并更新其值。所以你最终得到了一个带有最后一个值的dict。 您可以使用一个dict列表,例如:
相关问题 更多 >
编程相关推荐