-
Notifications
You must be signed in to change notification settings - Fork 0
/
confparse_ex7.py
43 lines (38 loc) · 1018 Bytes
/
confparse_ex7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from ciscoconfparse import CiscoConfParse
from pprint import pprint
bgp_config = """
router bgp 44
bgp router-id 10.220.88.38
address-family ipv4 unicast
!
neighbor 10.220.88.20
remote-as 42
description pynet-rtr1
address-family ipv4 unicast
route-policy ALLOW in
route-policy ALLOW out
!
!
neighbor 10.220.88.32
remote-as 43
address-family ipv4 unicast
route-policy ALLOW in
route-policy ALLOW out
"""
# When feeding config directly - CiscoConfParse requires a list
bgp_obj = CiscoConfParse(bgp_config.splitlines())
# Result of find_objects_w_parents will be the child objects
bgp_peers = []
neighbors = bgp_obj.find_objects_w_parents(
parentspec=r"router bgp", childspec=r"neighbor"
)
for neighbor in neighbors:
_, neighbor_ip = neighbor.text.split()
for child in neighbor.children:
if "remote-as" in child.text:
_, remote_as = child.text.split()
bgp_peers.append((neighbor_ip, remote_as))
print()
print("BGP Peers: ")
pprint(bgp_peers)
print()