8
8
9
9
'''
10
10
11
- import re
11
+ import re
12
+ import pprint
13
+
14
+
15
+ STRING = '''
16
+ Neighbor: 21.0.0.1, Address-Family: ipv4 Unicast (VRF1)
17
+ Locally configured policies:
18
+ route-map RMAP in
19
+ route-map RMAP2 out
20
+ Neighbor: 21.0.0.2, Address-Family: ipv6 Unicast (VRF2)
21
+ Locally configured policies:
22
+ route-map RMAP3 in
23
+ route-map RMAP4 out
24
+ Neighbor: 21.0.0.3, Address-Family: VPNv4 Unicast
25
+ Locally configured policies:
26
+ route-map RMAP5 in
27
+ route-map RMAP6 out
28
+ Neighbor: 21.0.0.4, Address-Family: VPNv6 Unicast (VRF2)
29
+ Locally configured policies:
30
+ route-map RMAP7 in
31
+ route-map RMAP8 out
32
+ '''
33
+
34
+ # initialize dictionary
35
+ PYTHON_DICT = {}
36
+
37
+ # split the output into seperate lines
38
+ for line in STRING .splitlines ():
39
+ # strip the line from any trailing white spaces
40
+ line = line .rstrip ()
41
+
42
+ # regexp to parse the below different outputs
43
+ # ** Neighbor: 21.0.0.1, Address-Family: ipv4 Unicast
44
+ # ** Neighbor: 21.0.0.2, Address-Family: VPNv4 Unicast (VRF2)
45
+ p1 = re .compile (r'^\s*Neighbor: +(?P<neighbor_name>[0-9\.]+),'
46
+ ' +Address-Family:'
47
+ ' +(?P<address_family_name>[a-zA-Z0-9\s]+)'
48
+ '( +\((?P<vrf_name>[a-zA-Z0-9]+)\))?$' )
49
+ # check if the regexp matched with the line
50
+ m = p1 .match (line )
51
+ if m :
52
+ # gather the prased keys
53
+ neighbor_name = m .groupdict ()['neighbor_name' ]
54
+ address_family_name = m .groupdict ()['address_family_name' ]
55
+ # check if vrf has been identified in the output, if not default
56
+ # it to 'default'
57
+ if m .groupdict ()['vrf_name' ]:
58
+ vrf_name = m .groupdict ()['vrf_name' ]
59
+ else :
60
+ vrf_name = 'default'
61
+ continue
62
+
63
+ # regexp to parse the below outputs
64
+ # ** route-map RMAP in
65
+ # ** route-map RMAP out
66
+ p2 = re .compile (r'\s*route-map +(?P<route_map_name>[a-zA-Z0-9]+) '
67
+ '+(?P<direction>[a-z]+)' )
68
+ # check if the regexp matched with the line
69
+ m = p2 .match (line )
70
+ if m :
71
+ # gather the prased keys
72
+ route_map_name = m .groupdict ()['route_map_name' ]
73
+ direction = m .groupdict ()['direction' ]
74
+
75
+ # build the desired structure/hierarchy out of the parsed output
76
+ if 'vrf' not in PYTHON_DICT :
77
+ PYTHON_DICT ['vrf' ] = {}
78
+ if vrf_name not in PYTHON_DICT ['vrf' ]:
79
+ PYTHON_DICT ['vrf' ][vrf_name ] = {}
80
+ if 'neighbor' not in PYTHON_DICT ['vrf' ][vrf_name ]:
81
+ PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ] = {}
82
+ if neighbor_name not in PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ]:
83
+ PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ][neighbor_name ] = {}
84
+ if 'address_family' not in PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ]\
85
+ [neighbor_name ]:
86
+ PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ][neighbor_name ]\
87
+ ['address_family' ] = {}
88
+ if address_family_name not in PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ]\
89
+ [neighbor_name ]['address_family' ]:
90
+ PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ][neighbor_name ]\
91
+ ['address_family' ][address_family_name ] = {}
92
+
93
+ if direction == 'in' :
94
+ PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ][neighbor_name ]\
95
+ ['address_family' ][address_family_name ]['route_map_in' ] = \
96
+ route_map_name
97
+ else :
98
+ PYTHON_DICT ['vrf' ][vrf_name ]['neighbor' ][neighbor_name ]\
99
+ ['address_family' ][address_family_name ]['route_map_out' ] = \
100
+ route_map_name
101
+
102
+ continue
103
+
104
+ # print the parsed built structure
105
+ pprint .pprint (PYTHON_DICT )
106
+
107
+ # Expected output
108
+ # ===============
109
+
110
+ # PYTHON_DICT = {
111
+ # 'vrf':
112
+ # {'VRF1':
113
+ # {'neighbor':
114
+ # {'21.0.0.1':
115
+ # {'address_family':
116
+ # {'VPNv4 Unicast':
117
+ # {'route_map_in': 'RMAP',
118
+ # 'route_map_out': 'RMAP2'}
119
+ # }
120
+ # }
121
+ # }
122
+ # },
123
+ # 'VRF2':
124
+ # {'neighbor':
125
+ # {'21.0.0.2':
126
+ # {'address_family':
127
+ # {'VPNv6 Unicast':
128
+ # {'route_map_in': 'RMAP3',
129
+ # 'route_map_out': 'RMAP4'}
130
+ # }
131
+ # },
132
+ # '21.0.0.4':
133
+ # {'address_family':
134
+ # {'VPNv6 Unicast':
135
+ # {'route_map_in': 'RMAP7',
136
+ # 'route_map_out': 'RMAP8'}
137
+ # }
138
+ # }
139
+ # }
140
+ # },
141
+ # 'default':
142
+ # {'neighbor':
143
+ # {'21.0.0.3':
144
+ # {'address_family':
145
+ # {'VPNv4 Unicast':
146
+ # {'route_map_in': 'RMAP5',
147
+ # 'route_map_out': 'RMAP6'}
148
+ # }
149
+ # }
150
+ # }
151
+ # }
152
+ # }
153
+ # }
0 commit comments