cisco-config-parser 2.0__tar.gz → 2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/PKG-INFO +109 -15
  2. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/README.md +108 -14
  3. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/__init__.py +2 -1
  4. cisco_config_parser-2.2/cisco_config_parser/cisco_config_parser.py +3 -0
  5. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/global_search/global_parser.py +3 -1
  6. cisco_config_parser-2.2/cisco_config_parser/helper/help.py +73 -0
  7. cisco_config_parser-2.2/cisco_config_parser/helper/helper.py +23 -0
  8. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer3_interface/l3_interface_obj.py +2 -1
  9. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer3_interface/l3_interface_parser.py +27 -0
  10. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer3_interface/l3_section_parser.py +25 -0
  11. cisco_config_parser-2.2/cisco_config_parser/old_version/__init__.py +4 -0
  12. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/old_version/utils.py +1 -1
  13. cisco_config_parser-2.2/cisco_config_parser/parser/__init__.py +144 -0
  14. cisco_config_parser-2.2/cisco_config_parser/parser/parser.py +201 -0
  15. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/parser_regex/regex.py +46 -1
  16. cisco_config_parser-2.2/cisco_config_parser/routing_protocol/__init__.py +4 -0
  17. cisco_config_parser-2.0/cisco_config_parser/testing.py → cisco_config_parser-2.2/cisco_config_parser/routing_protocol/rtp_dynamic_obj.py +0 -0
  18. cisco_config_parser-2.2/cisco_config_parser/routing_protocol/rtp_dynamic_parser.py +0 -0
  19. cisco_config_parser-2.2/cisco_config_parser/routing_protocol/rtp_ios_obj.py +68 -0
  20. cisco_config_parser-2.2/cisco_config_parser/routing_protocol/rtp_ios_parser.py +448 -0
  21. cisco_config_parser-2.2/cisco_config_parser/routing_protocol/rtp_separator.py +130 -0
  22. cisco_config_parser-2.2/cisco_config_parser/testing.py +0 -0
  23. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser.egg-info/PKG-INFO +109 -15
  24. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser.egg-info/SOURCES.txt +9 -0
  25. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/setup.py +1 -1
  26. cisco_config_parser-2.0/cisco_config_parser/cisco_config_parser.py +0 -2
  27. cisco_config_parser-2.0/cisco_config_parser/parser/__init__.py +0 -78
  28. cisco_config_parser-2.0/cisco_config_parser/parser/parser.py +0 -101
  29. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/global_search/__init__.py +0 -0
  30. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/global_search/global_obj.py +0 -0
  31. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/global_search/global_separator.py +0 -0
  32. {cisco_config_parser-2.0/cisco_config_parser/old_version → cisco_config_parser-2.2/cisco_config_parser/helper}/__init__.py +0 -0
  33. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer2_interface/__init__.py +0 -0
  34. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer2_interface/l2_interface_obj.py +0 -0
  35. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer2_interface/l2_interface_parser.py +1 -1
  36. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer2_interface/l2_interface_separator.py +0 -0
  37. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer2_interface/l2_section_parser.py +0 -0
  38. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer3_interface/__init__.py +0 -0
  39. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/layer3_interface/l3_interface_separator.py +0 -0
  40. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/old_version/exceptions.py +0 -0
  41. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/old_version/obj.py +0 -0
  42. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/old_version/old_parse.py +0 -0
  43. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/old_version/ssh.py +0 -0
  44. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/parent_child/__init__.py +0 -0
  45. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/parent_child/parent_child_obj.py +0 -0
  46. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/parent_child/parent_child_parser.py +0 -0
  47. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/parent_child/parent_child_separator.py +0 -0
  48. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/parser_regex/__init__.py +0 -0
  49. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/separator/__init__.py +0 -0
  50. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/separator/separator.py +0 -0
  51. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser/setup.py +0 -0
  52. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser.egg-info/dependency_links.txt +0 -0
  53. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/cisco_config_parser.egg-info/top_level.txt +0 -0
  54. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/pyproject.toml +0 -0
  55. {cisco_config_parser-2.0 → cisco_config_parser-2.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cisco_config_parser
3
- Version: 2.0
3
+ Version: 2.2
4
4
  Summary: This Package Will Parse Cisco IOS, IOS-XE, IOS-XR and NXOS Configuration File.
5
5
  Home-page: https://github.com/arezazadeh/cisco_config_parser
6
6
  Author: Ahmad Rezazadeh
@@ -12,7 +12,89 @@ Description-Content-Type: text/markdown
12
12
  ## This Package Will Parse Cisco IOS, IOS-XE, IOS-XR, and NXOS Configuration File.
13
13
 
14
14
 
15
- # New Features
15
+ # New Version 2.0
16
+
17
+ ## Classes in this library
18
+
19
+ ```python
20
+ IOSRouteParser
21
+ L3InterfaceParser
22
+ L2InterfaceParser
23
+ ParentChildParser
24
+ ```
25
+ there more Classes are being built, and will be released in the upcoming versions.
26
+
27
+ ## Get Example usage:
28
+
29
+ ```python
30
+ from cisco_config_parser.helper import helper
31
+
32
+ helper = helper.IOSRouteParser()
33
+
34
+ print(helper)
35
+ ```
36
+
37
+ **Output**:
38
+ ```
39
+
40
+ Example Usage:
41
+ ====================================================================================================
42
+
43
+ with open("show_run.txt", "r") as file_output:
44
+ content = file_output.read()
45
+
46
+
47
+ obj = ConfigParser(content)
48
+
49
+
50
+ #
51
+ ##
52
+ ###
53
+ #### Static Routes Example:
54
+ ###
55
+ ##
56
+ #
57
+ obj.get_static_routes()
58
+ obj.get_static_routes(return_json=True)
59
+
60
+
61
+ #
62
+ ##
63
+ ###
64
+ #### OSPF Example:
65
+ ###
66
+ ##
67
+ #
68
+ ospf = obj.get_ospf_config(return_json=True)
69
+ ospf = obj.get_ospf_config()
70
+ for i in ospf:
71
+ print(i.children)
72
+ print(i.network)
73
+ print(i.no_passive_interface)
74
+
75
+ #
76
+ ##
77
+ ###
78
+ #### EIGRP Example:
79
+ ###
80
+ ##
81
+ #
82
+ eigrp = obj.get_eigrp_config(return_json=True)
83
+ eigrp = obj.get_eigrp_config()
84
+ for i in eigrp:
85
+ print(i.children)
86
+ print(i.network)
87
+ print(i.vrf_children) << returns a list of EIGRPVrfChildren objects
88
+ vrf_children = i.vrf_children
89
+ for vrf in vrf_children:
90
+ print(vrf.network)
91
+ print(vrf.children)
92
+
93
+
94
+ ```
95
+
96
+
97
+
16
98
 
17
99
  ### 1. Layer3 Interfaces:
18
100
  - added interface details - returns dict of all layer3 interfaces
@@ -20,9 +102,12 @@ Description-Content-Type: text/markdown
20
102
  - platform agnostic
21
103
  - able to set custom regex and create dynamic method
22
104
 
105
+
23
106
  * Example:
24
107
 
25
108
  ```python
109
+ from cisco_config_parser import ConfigParser
110
+
26
111
  l3_intf = ConfigParser(res)
27
112
  l3 = l3_intf.get_l3_interfaces(ip_pim="ip pim *", ospf="ip ospf.*")
28
113
 
@@ -51,12 +136,20 @@ None
51
136
  <hr>
52
137
  <hr>
53
138
 
139
+ # Old Version < 2.0
54
140
 
55
141
  There are two ways to parse the config, 1, SSH which is not recommended, and 2, feeding the running-config file
142
+
56
143
  * to use file, use `ConfigParser(method="file", content=<your_file>, json=True/False)`.
57
144
  * to use SSH:
58
- ```ruby
59
- ConfigParser(
145
+
146
+
147
+ ```python
148
+ from cisco_config_parser import ConfigParserOld
149
+
150
+
151
+
152
+ ConfigParserOld(
60
153
  method="int_ssh",
61
154
  ssh=True,
62
155
  user="username",
@@ -112,7 +205,8 @@ parse.find_parent_child("^.*Loopback")
112
205
 
113
206
 
114
207
  ```ruby
115
- >>> nxos_parser = ConfigParser(method="file", content=file1)
208
+
209
+ >>> nxos_parser = ConfigParserOld(method="file", content=file1)
116
210
  >>> vlan_info = nxos_parser.nxos_get_vlan_info()
117
211
  >>> vlan_info.vlan = "2626"
118
212
  >>> print(vlan_info.vlan)
@@ -149,11 +243,11 @@ evpn
149
243
 
150
244
  ```ruby
151
245
 
152
- from cisco_config_parser import ConfigParser
246
+ from cisco_config_parser import ConfigParserOld
153
247
 
154
248
  my_file = "running-config.txt"
155
249
 
156
- parser = ConfigParser(method="file", content=my_file)
250
+ parser = ConfigParserOld(method="file", content=my_file)
157
251
 
158
252
  obj = parser.ios_get_routed_port()
159
253
 
@@ -191,11 +285,11 @@ there are two different mode on switchport, `access` and `trunk`. you should spe
191
285
 
192
286
  ```ruby
193
287
 
194
- from cisco_config_parser import ConfigParser
288
+ from cisco_config_parser import ConfigParserOld
195
289
 
196
290
  my_file = "switch01_run_config.txt"
197
291
 
198
- parser = ConfigParser(method="file", content=my_file)
292
+ parser = ConfigParserOld(method="file", content=my_file)
199
293
 
200
294
  obj = parser.ios_get_switchport(mode="access")
201
295
 
@@ -229,11 +323,11 @@ Voice vlan 700
229
323
 
230
324
  * Finding Routing Protocol
231
325
  ```ruby
232
- from cisco_config_parser import ConfigParser
326
+ from cisco_config_parser import ConfigParserOld
233
327
 
234
328
 
235
329
  my_file = "switch01_running_config.txt"
236
- parse = ConfigParser(method="file", content=my_file)
330
+ parse = ConfigParserOld(method="file", content=my_file)
237
331
 
238
332
 
239
333
  obj_list = parse.find_parent_child("^router")
@@ -271,11 +365,11 @@ Voice vlan 700
271
365
  * Finding Interface and Helper address Example
272
366
 
273
367
  ```ruby
274
- from cisco_config_parser import ConfigParser
368
+ from cisco_config_parser import ConfigParserOld
275
369
 
276
370
 
277
371
  my_file = "switch01_running_config.txt"
278
- parse = ConfigParser(method="file", content=my_file)
372
+ parse = ConfigParserOld(method="file", content=my_file)
279
373
  obj_list = parse.find_parent_child("^interface")
280
374
 
281
375
  for i in obj_list:
@@ -298,12 +392,12 @@ interface Vlan200
298
392
 
299
393
  ```ruby
300
394
 
301
- from cisco_config_parser import ConfigParser
395
+ from cisco_config_parser import ConfigParserOld
302
396
 
303
397
 
304
398
 
305
399
  my_file = "switch_01-run_config.txt"
306
- parser = ConfigParser(method="file", content=my_file)
400
+ parser = ConfigParserOld(method="file", content=my_file)
307
401
 
308
402
  res = parser.ios_get_svi_objects()
309
403
 
@@ -2,7 +2,89 @@
2
2
  ## This Package Will Parse Cisco IOS, IOS-XE, IOS-XR, and NXOS Configuration File.
3
3
 
4
4
 
5
- # New Features
5
+ # New Version 2.0
6
+
7
+ ## Classes in this library
8
+
9
+ ```python
10
+ IOSRouteParser
11
+ L3InterfaceParser
12
+ L2InterfaceParser
13
+ ParentChildParser
14
+ ```
15
+ there more Classes are being built, and will be released in the upcoming versions.
16
+
17
+ ## Get Example usage:
18
+
19
+ ```python
20
+ from cisco_config_parser.helper import helper
21
+
22
+ helper = helper.IOSRouteParser()
23
+
24
+ print(helper)
25
+ ```
26
+
27
+ **Output**:
28
+ ```
29
+
30
+ Example Usage:
31
+ ====================================================================================================
32
+
33
+ with open("show_run.txt", "r") as file_output:
34
+ content = file_output.read()
35
+
36
+
37
+ obj = ConfigParser(content)
38
+
39
+
40
+ #
41
+ ##
42
+ ###
43
+ #### Static Routes Example:
44
+ ###
45
+ ##
46
+ #
47
+ obj.get_static_routes()
48
+ obj.get_static_routes(return_json=True)
49
+
50
+
51
+ #
52
+ ##
53
+ ###
54
+ #### OSPF Example:
55
+ ###
56
+ ##
57
+ #
58
+ ospf = obj.get_ospf_config(return_json=True)
59
+ ospf = obj.get_ospf_config()
60
+ for i in ospf:
61
+ print(i.children)
62
+ print(i.network)
63
+ print(i.no_passive_interface)
64
+
65
+ #
66
+ ##
67
+ ###
68
+ #### EIGRP Example:
69
+ ###
70
+ ##
71
+ #
72
+ eigrp = obj.get_eigrp_config(return_json=True)
73
+ eigrp = obj.get_eigrp_config()
74
+ for i in eigrp:
75
+ print(i.children)
76
+ print(i.network)
77
+ print(i.vrf_children) << returns a list of EIGRPVrfChildren objects
78
+ vrf_children = i.vrf_children
79
+ for vrf in vrf_children:
80
+ print(vrf.network)
81
+ print(vrf.children)
82
+
83
+
84
+ ```
85
+
86
+
87
+
6
88
 
7
89
  ### 1. Layer3 Interfaces:
8
90
  - added interface details - returns dict of all layer3 interfaces
@@ -10,9 +92,12 @@
10
92
  - platform agnostic
11
93
  - able to set custom regex and create dynamic method
12
94
 
95
+
13
96
  * Example:
14
97
 
15
98
  ```python
99
+ from cisco_config_parser import ConfigParser
100
+
16
101
  l3_intf = ConfigParser(res)
17
102
  l3 = l3_intf.get_l3_interfaces(ip_pim="ip pim *", ospf="ip ospf.*")
18
103
 
@@ -41,12 +126,20 @@ None
41
126
  <hr>
42
127
  <hr>
43
128
 
129
+ # Old Version < 2.0
44
130
 
45
131
  There are two ways to parse the config, 1, SSH which is not recommended, and 2, feeding the running-config file
132
+
46
133
  * to use file, use `ConfigParser(method="file", content=<your_file>, json=True/False)`.
47
134
  * to use SSH:
48
- ```ruby
49
- ConfigParser(
135
+
136
+
137
+ ```python
138
+ from cisco_config_parser import ConfigParserOld
139
+
140
+
141
+
142
+ ConfigParserOld(
50
143
  method="int_ssh",
51
144
  ssh=True,
52
145
  user="username",
@@ -102,7 +195,8 @@ parse.find_parent_child("^.*Loopback")
102
195
 
103
196
 
104
197
  ```ruby
105
- >>> nxos_parser = ConfigParser(method="file", content=file1)
198
+
199
+ >>> nxos_parser = ConfigParserOld(method="file", content=file1)
106
200
  >>> vlan_info = nxos_parser.nxos_get_vlan_info()
107
201
  >>> vlan_info.vlan = "2626"
108
202
  >>> print(vlan_info.vlan)
@@ -139,11 +233,11 @@ evpn
139
233
 
140
234
  ```ruby
141
235
 
142
- from cisco_config_parser import ConfigParser
236
+ from cisco_config_parser import ConfigParserOld
143
237
 
144
238
  my_file = "running-config.txt"
145
239
 
146
- parser = ConfigParser(method="file", content=my_file)
240
+ parser = ConfigParserOld(method="file", content=my_file)
147
241
 
148
242
  obj = parser.ios_get_routed_port()
149
243
 
@@ -181,11 +275,11 @@ there are two different mode on switchport, `access` and `trunk`. you should spe
181
275
 
182
276
  ```ruby
183
277
 
184
- from cisco_config_parser import ConfigParser
278
+ from cisco_config_parser import ConfigParserOld
185
279
 
186
280
  my_file = "switch01_run_config.txt"
187
281
 
188
- parser = ConfigParser(method="file", content=my_file)
282
+ parser = ConfigParserOld(method="file", content=my_file)
189
283
 
190
284
  obj = parser.ios_get_switchport(mode="access")
191
285
 
@@ -219,11 +313,11 @@ Voice vlan 700
219
313
 
220
314
  * Finding Routing Protocol
221
315
  ```ruby
222
- from cisco_config_parser import ConfigParser
316
+ from cisco_config_parser import ConfigParserOld
223
317
 
224
318
 
225
319
  my_file = "switch01_running_config.txt"
226
- parse = ConfigParser(method="file", content=my_file)
320
+ parse = ConfigParserOld(method="file", content=my_file)
227
321
 
228
322
 
229
323
  obj_list = parse.find_parent_child("^router")
@@ -261,11 +355,11 @@ Voice vlan 700
261
355
  * Finding Interface and Helper address Example
262
356
 
263
357
  ```ruby
264
- from cisco_config_parser import ConfigParser
358
+ from cisco_config_parser import ConfigParserOld
265
359
 
266
360
 
267
361
  my_file = "switch01_running_config.txt"
268
- parse = ConfigParser(method="file", content=my_file)
362
+ parse = ConfigParserOld(method="file", content=my_file)
269
363
  obj_list = parse.find_parent_child("^interface")
270
364
 
271
365
  for i in obj_list:
@@ -288,12 +382,12 @@ interface Vlan200
288
382
 
289
383
  ```ruby
290
384
 
291
- from cisco_config_parser import ConfigParser
385
+ from cisco_config_parser import ConfigParserOld
292
386
 
293
387
 
294
388
 
295
389
  my_file = "switch_01-run_config.txt"
296
- parser = ConfigParser(method="file", content=my_file)
390
+ parser = ConfigParserOld(method="file", content=my_file)
297
391
 
298
392
  res = parser.ios_get_svi_objects()
299
393
 
@@ -1,8 +1,9 @@
1
- from .cisco_config_parser import ConfigParser
1
+ from .cisco_config_parser import ConfigParser, ConfigParserOld
2
2
 
3
3
 
4
4
  _ALL_ = [
5
5
  "ConfigParser"
6
+ "ConfigParserOld",
6
7
  ]
7
8
 
8
9
  __version__ = "1.4.6"
@@ -0,0 +1,3 @@
1
+
2
+ from .parser import ConfigParser
3
+ from .parser import ConfigParserOld
@@ -52,4 +52,6 @@ class GlobalParser:
52
52
  ]
53
53
  vlan_objects.append(vlan_obj)
54
54
 
55
- return vlan_objects
55
+ return vlan_objects
56
+
57
+
@@ -0,0 +1,73 @@
1
+
2
+
3
+ RTP_IOS_HELP = """
4
+
5
+ Example Usage:
6
+ ====================================================================================================
7
+
8
+ with open("show_run.txt", "r") as file_output:
9
+ content = file_output.read()
10
+
11
+
12
+ obj = ConfigParser(content)
13
+
14
+
15
+ #
16
+ ##
17
+ ###
18
+ #### Static Routes Example:
19
+ ###
20
+ ##
21
+ #
22
+ obj.get_static_routes()
23
+ obj.get_static_routes(return_json=True)
24
+
25
+
26
+ #
27
+ ##
28
+ ###
29
+ #### OSPF Example:
30
+ ###
31
+ ##
32
+ #
33
+ ospf = obj.get_ospf_config(return_json=True)
34
+ ospf = obj.get_ospf_config()
35
+ for i in ospf:
36
+ print(i.children)
37
+ print(i.network)
38
+ print(i.no_passive_interface)
39
+
40
+ #
41
+ ##
42
+ ###
43
+ #### EIGRP Example:
44
+ ###
45
+ ##
46
+ #
47
+ eigrp = obj.get_eigrp_config(return_json=True)
48
+ eigrp = obj.get_eigrp_config()
49
+ for i in eigrp:
50
+ print(i.children)
51
+ print(i.network)
52
+ print(i.vrf_children) << returns a list of EIGRPVrfChildren objects
53
+ vrf_children = i.vrf_children
54
+ for vrf in vrf_children:
55
+ print(vrf.network)
56
+ print(vrf.children)
57
+ """
58
+ PARENT_CHILD_HELP = """
59
+ Example Usage:
60
+ ====================================================================================================
61
+
62
+ with open("show_run.txt", "r") as file_output:
63
+ content = file_output.read()
64
+
65
+ obj = ConfigParser(content)
66
+
67
+ parent_child = obj.get_parent_child(custom_regex="(^snmp.*)|(^aaa.*)")
68
+
69
+ for i in parent_child:
70
+ print(i.parent)
71
+ print(i.children)
72
+
73
+ """
@@ -0,0 +1,23 @@
1
+ from .help import *
2
+
3
+
4
+ class IOSRouteParser:
5
+ def __repr__(self):
6
+ return RTP_IOS_HELP
7
+
8
+
9
+
10
+ class ParentChild:
11
+ def __repr__(self):
12
+ return PARENT_CHILD_HELP
13
+
14
+
15
+ class L3Interface:
16
+ def __repr__(self):
17
+ return "L3 Interface Usage"
18
+
19
+
20
+
21
+
22
+
23
+
@@ -32,4 +32,5 @@ class L3Interface:
32
32
  """
33
33
  If the attribute is not found, return None
34
34
  """
35
- return None
35
+ return None
36
+
@@ -1,8 +1,13 @@
1
+ import json
2
+
1
3
  from .l3_interface_separator import L3InterfaceSeparator
2
4
  from .l3_interface_obj import L3Interface
3
5
  from .l3_section_parser import _L3SectionParser
4
6
  from cisco_config_parser.parser_regex.regex import *
5
7
  from dataclasses import dataclass
8
+ import re, ipaddress
9
+
10
+
6
11
 
7
12
 
8
13
 
@@ -65,6 +70,7 @@ class L3InterfaceParser:
65
70
  if ip_address_regex:
66
71
  l3_intf_cls = _L3SectionParser._parse_ip_address_line(**ip_address_kwargs)
67
72
 
73
+
68
74
  if sec_ip_address_regex:
69
75
  # set the primary flag to False to indicate that the ip address is a secondary ip address
70
76
  ip_address_kwargs["primary"] = False
@@ -102,5 +108,26 @@ class L3InterfaceParser:
102
108
  return l3_intf_objects
103
109
 
104
110
 
111
+ def _fetch_subnet_and_usage(self, include_subnet_count=False):
112
+ """
113
+ Fetch the subnet usage from the config file
114
+ return: dictionary of subnet usage
115
+ """
105
116
 
117
+ subnet_usage = {}
118
+ subnet_count = {}
119
+ l3_interfaces = self._fetch_l3_interfaces()
120
+ for i in l3_interfaces:
121
+ if i.subnet:
122
+ cidr = str(ipaddress.IPv4Network(i.subnet, strict=False).prefixlen)
123
+ subnet_usage[i.name] = i.subnet
124
+ if subnet_count.get(f"/{cidr}"):
125
+ subnet_count[f"/{cidr}"] += 1
126
+ else:
127
+ subnet_count[f"/{cidr}"] = 1
128
+
129
+ if include_subnet_count:
130
+ subnet_usage["subnet_count"] = subnet_count
131
+
132
+ return subnet_usage
106
133
 
@@ -5,6 +5,8 @@ import ipaddress
5
5
 
6
6
  class _L3SectionParser:
7
7
 
8
+
9
+
8
10
  @classmethod
9
11
  def _parse_ip_address_line(cls, **kwargs):
10
12
  """
@@ -16,11 +18,34 @@ class _L3SectionParser:
16
18
  mask using the ipaddress module
17
19
  return: l3_interface_obj
18
20
  """
21
+
22
+ def process_ip_address_with_cidr(ip_address_with_cidr, obj):
23
+ """
24
+ Process the ip address with cidr
25
+ :param ip_address_with_cidr: str
26
+ :param obj: L3Interface object
27
+ :return:
28
+ """
29
+ ip_address_config = ip_address_with_cidr.split("/")
30
+ ip_addr = ip_address_config[0].strip()
31
+ cidr = ip_address_config[1].strip()
32
+ subnet_ = ipaddress.IPv4Network(f"{ip_addr}/{cidr}", strict=False)
33
+ obj.ip_address = ip_addr.strip()
34
+ obj.mask = str(subnet_.netmask)
35
+ obj.subnet = str(subnet_).strip()
36
+ return obj
37
+
19
38
  ip_address_line_regex = kwargs.get("ip_address_line_regex")
20
39
  l3_interface_obj = kwargs.get("l3_interface_obj")
21
40
  primary = kwargs.get("primary", True)
22
41
 
23
42
  ip_address_regex = [ip_group for ip_group in ip_address_line_regex.groups() if ip_group]
43
+
44
+ # Check if the ip address has a CIDR - 10.1.1.1/24
45
+ if "/" in ip_address_regex[0]:
46
+ l3_interface_obj = process_ip_address_with_cidr(ip_address_regex[0], l3_interface_obj)
47
+ return l3_interface_obj
48
+
24
49
  ip_address_section = ip_address_regex[0].split()
25
50
  ip_address = ip_address_section[0].strip()
26
51
  mask = ip_address_section[1].strip()
@@ -0,0 +1,4 @@
1
+ from .utils import *
2
+ from .exceptions import *
3
+ from .obj import *
4
+ from .old_parse import *
@@ -3,7 +3,7 @@ from .ssh import *
3
3
  from .old_parse import *
4
4
 
5
5
 
6
- class Parser_backup:
6
+ class OldParser:
7
7
  """
8
8
  - Example: Finding Routing Protocol
9
9
  my_file = "switch01_running_config.txt"