【发布时间】:2020-06-20 06:02:37
【问题描述】:
我正在尝试使用 ciscoconfparse 将邻居、远程身份和描述提取到字典中。但是其中一个邻居没有描述。因此它不会返回值
任何人都可以帮助获取所有邻居值的正确方法
Config:
router bgp 42098001
neighbor SERVER peer-group
neighbor SERVER remote-as 64700
neighbor 10.29.0.65 remote-as 1111
neighbor 10.29.0.65 description to ZZZ
neighbor 10.29.0.73 remote-as 2222
neighbor 10.29.0.73 description to AAA
neighbor 10.29.0.81 remote-as 3333
neighbor 10.29.0.81 description to BBB
neighbor 10.29.0.90 remote-as 4209800190
neighbor 10.29.0.90 description to ABC
neighbor 10.232.122.170 remote-as 64700
neighbor 10.232.122.170 description ABD
neighbor 10.237.34.2 remote-as 4209800192
neighbor 10.237.34.2 description to CCC
bgp_as_name = confparse.find_all_children(r"^router bgp")
for details in bgp_as_name:
if 'remote-as' in details:
remote_ip = details.strip().rsplit(' ')[1]
as_number = details.strip().rsplit(' ')[3]
#print(remote_ip)
if 'description' in details:
description = details.strip().rsplit(' ')[3:]
desc = (' ').join(description)
bgp_as_ip.update({'description': desc})
print(bgp_as_ip)
#BGP route-map
bgp_route_map = confparse.find_all_children(r"^router bgp")
for routemap in bgp_route_map:
if 'route-map' in routemap:
bgp_routemap_slice1 = routemap.strip().split(' ')[0:2]
bgp_routemap_slice2 = routemap.strip().split(' ')[-2:]
bgp_routemap_combine = bgp_routemap_slice1 + bgp_routemap_slice2
bgp_route_map = bgp_routemap_combine[1:4]
print(bgp_route_map)
#bgp_as_ip.update({'route-map': bgp_route_map})
#print(bgp_as_ip)
RESULT
{'remote_ip': '10.29.0.65', 'as_num': '1111', 'description': 'to ZZZ'}
{'remote_ip': '10.29.0.73', 'as_num': '2222', 'description': 'to AAA'}
{'remote_ip': '10.29.0.81', 'as_num': '3333', 'description': 'to BBB'}
{'remote_ip': '10.29.0.90', 'as_num': '4201', 'description': 'to ABC'}
{'remote_ip': '10.232.122.170', 'as_num': '64700', 'description': 'ABD'}
{'remote_ip': '10.237.34.2', 'as_num': '4209', 'description': 'to CCC'}
>缺少的信息是邻居SERVER
【问题讨论】:
-
SERVER 不是“真正的”邻居;它是一个对等组。此外,即使对于真正的邻居,描述也不是强制性的,因此您应该处理没有描述的邻居的情况。
-
感谢您的回复。我的目的是获取邻居信息(对等/远程,如,描述)并将其放入 excel 文件。我缺少配置中没有描述的邻居信息。如果没有描述,我试图输入“none”,但结果 bgp 配置的其余部分(如密码、路由映射)也得到“none”值。
-
你还有这个问题吗?如果没有,请继续发布您的解决方案,以免他们的问题没有得到解答
标签: python python-3.x list ciscoconfparse