@@ -14,6 +14,7 @@ Save this code in a file named example.py
14
14
from ciscoconfparse2 import CiscoConfParse
15
15
from ciscoconfparse2 import IPv4Obj
16
16
17
+
17
18
def intf_csv (intf_obj ) -> str :
18
19
"""
19
20
:return: CSV for each interface object.
@@ -24,44 +25,48 @@ def intf_csv(intf_obj) -> str:
24
25
# something like 'interface Loopback0'
25
26
intf_name = " " .join(intf_obj.split()[1 :])
26
27
27
- admin_status = intf_obj.re_match_iter_typed(" ^\s+(shutdown) " ,
28
- default = " not_shutdown" ,
29
- result_type = str )
28
+ admin_status = intf_obj.re_match_iter_typed(
29
+ " ^\s+(shutdown) " , default = " not_shutdown" , result_type = str
30
+ )
30
31
31
32
# Search children of all interfaces for a regex match and return
32
33
# the value matched in regex match group 1. If there is no match,
33
34
# return a default value: 0.0.0.1/32
34
- addr_netmask = intf_obj.re_match_iter_typed(r " ^ \s + ip\s address\s ( \d + \. \d + \. \d + \. \d + \s\S + ) " ,
35
- result_type = IPv4Obj,
36
- group = 1 ,
37
- default = IPv4Obj(" 0.0.0.1/32" ))
35
+ addr_netmask = intf_obj.re_match_iter_typed(
36
+ r " ^ \s + ip\s address\s ( \d + \. \d + \. \d + \. \d + \s\S + ) " ,
37
+ result_type = IPv4Obj,
38
+ group = 1 ,
39
+ default = IPv4Obj(" 0.0.0.1/32" ),
40
+ )
38
41
39
42
# Find the description and replace all commas in it
40
43
description = intf_obj.re_match_iter_typed(" description\s+(\S.*)" ).replace(" ," , " _" )
41
44
42
- switchport_status = intf_obj.re_match_iter_typed(" (switchport)" ,
43
- default = " not_switched" )
45
+ switchport_status = intf_obj.re_match_iter_typed(
46
+ " (switchport)" , default = " not_switched"
47
+ )
44
48
45
49
# Return a csv based on whether this is a switchport
46
50
if switchport_status == " not_switched" :
47
51
return f " { intf_name} , { admin_status} , { addr_netmask.as_cidr_addr} , { switchport_status} ,,, { description} "
48
52
49
53
else :
50
54
# Only calculate switchport values if this is a switchport
51
- trunk_access = intf_obj.re_match_iter_typed(" switchport mode (trunk) " ,
52
- default = " access" ,
53
- result_type = str )
54
- access_vlan = intf_obj.re_match_iter_typed(" switchport access vlan (\d+) " ,
55
- default = 1 ,
56
- result_type = int )
55
+ trunk_access = intf_obj.re_match_iter_typed(
56
+ " switchport mode (trunk) " , default = " access" , result_type = str
57
+ )
58
+ access_vlan = intf_obj.re_match_iter_typed(
59
+ " switchport access vlan (\d+) " , default = 1 , result_type = int
60
+ )
57
61
58
62
# Return the CSV string of values...
59
63
return f " { intf_name} , { admin_status} , { switchport_status} , { trunk_access} , { access_vlan} , { description} "
60
64
61
- parse = CiscoConfParse(' tests/fixtures/configs/sample_08.ios' , syntax = ' ios' )
65
+
66
+ parse = CiscoConfParse(" tests/fixtures/configs/sample_08.ios" , syntax = " ios" )
62
67
63
68
# Find interface BaseCfgLine() instances...
64
- for intf_obj in parse.find_objects(' ^interface' ):
69
+ for intf_obj in parse.find_objects(" ^interface" ):
65
70
print (intf_csv(intf_obj))
66
71
```
67
72
0 commit comments