Skip to content

Commit 845cabd

Browse files
committed
add implement HSRP using ciscoconfparse example script
1 parent 60974d7 commit 845cabd

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
vlan 100
3+
name Data_100
4+
!
5+
vlan 201
6+
name Data_201
7+
!
8+
vlan 203
9+
name Data_203
10+
!
11+
vlan 204
12+
name Data_204
13+
!
14+
vlan 300
15+
name Server_300
16+
!
17+
vlan 301
18+
name Server_301
19+
!
20+
vlan 310
21+
name Server_310
22+
!
23+
vlan 320
24+
name Server_320
25+
!
26+
vlan 351
27+
name Server_351
28+
!
29+
vlan 352
30+
name Server_352
31+
!
32+
interface Vlan100
33+
ip address 10.0.100.1 255.255.255.0
34+
!
35+
interface Vlan201
36+
ip address 10.0.200.254 255.255.255.0
37+
!
38+
interface Vlan203
39+
ip address 10.0.203.1 255.255.255.0
40+
!
41+
interface Vlan204
42+
ip address 10.0.204.1 255.255.255.0
43+
!
44+
interface Vlan300
45+
ip address 10.10.0.128 255.255.255.0
46+
!
47+
interface Vlan301
48+
ip address 10.10.1.1 255.255.255.0
49+
!
50+
interface Vlan310
51+
ip address 10.10.10.1 255.255.255.0
52+
!
53+
interface Vlan320
54+
ip address 10.10.20.126 255.255.255.128
55+
!
56+
interface Vlan351
57+
ip address 10.10.51.126 255.255.255.128
58+
!
59+
interface Vlan352
60+
ip address 10.10.52.126 255.255.255.128
61+
!
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import sys
3+
from ipaddress import IPv4Network
4+
from ciscoconfparse import CiscoConfParse
5+
from ciscoconfparse.ccp_util import IPv4Obj
6+
7+
8+
output_directory = "_output"
9+
input_configuration_file = "cisco_ios_vlans.txt"
10+
primary_configuration_file = "primary_config.txt"
11+
secondary_configuration_file = "secondary_config.txt"
12+
13+
14+
if __name__ == "__main__":
15+
# just make sure that the output directory exists
16+
if not os.path.exists(output_directory):
17+
os.mkdir(output_directory)
18+
19+
# load the input Cisco IOS configuration file
20+
print("load Cisco IOS configuration file...")
21+
try:
22+
parsed_config = CiscoConfParse(input_configuration_file)
23+
24+
except Exception as ex:
25+
print(ex)
26+
sys.exit()
27+
28+
# create objects for diff
29+
print("create primary and secondary configuration object...")
30+
primary_config = CiscoConfParse([
31+
"!",
32+
"! primary switch interface configuration",
33+
"!",
34+
])
35+
secondary_config = CiscoConfParse([
36+
"!",
37+
"! secondary switch interface configuration",
38+
"!",
39+
])
40+
41+
# read all VLAN SVI's which has an ip address
42+
vlan_interfaces = parsed_config.find_objects_w_child("^interface Vlan", r"^ ip address.*")
43+
44+
for vlan_interface in vlan_interfaces:
45+
# determine current ip address
46+
ipv4_addr = vlan_interface.re_match_iter_typed(r"ip\saddress\s(\S+\s+\S+)", result_type=IPv4Obj)
47+
48+
# determine the current interface name and VLAN ID
49+
vlan_interface_string = vlan_interface.text.lstrip("interface ")
50+
vlan_id = vlan_interface_string.lstrip("Vlan")
51+
52+
# the current SVI address is used as a HSRP virtual IP
53+
virtual_ip = ipv4_addr.ip_object
54+
55+
# at this point we need to determine the next addresses which are used for the primary and secondary switch
56+
# we will try, if the next two addresses are part of the network, otherwise we will use the previous two
57+
# addresses
58+
ipv4_network = IPv4Network(ipv4_addr.network)
59+
if (ipv4_addr.ip_object + 1) in ipv4_network.hosts():
60+
primary_ip = ipv4_addr.ip_object + 1
61+
secondary_ip = ipv4_addr.ip_object + 2
62+
else:
63+
primary_ip = ipv4_addr.ip_object - 1
64+
secondary_ip = ipv4_addr.ip_object - 2
65+
66+
# now add the configuration to the change scripts
67+
primary_config.append_line("interface %s" % vlan_interface_string)
68+
primary_config.append_line(" description *** VLAN SVI %s" % vlan_id)
69+
primary_config.append_line(" ip address %s %s" % (primary_ip, ipv4_addr.netmask))
70+
primary_config.append_line(" standby version 2")
71+
primary_config.append_line(" standby 1 ip %s" % virtual_ip)
72+
primary_config.append_line(" standby 1 priority 255")
73+
primary_config.append_line(" standby 1 authentication md5 key-string vl%s" % vlan_id)
74+
primary_config.append_line("!")
75+
76+
secondary_config.append_line("interface %s" % vlan_interface_string)
77+
secondary_config.append_line(" description *** VLAN SVI %s" % vlan_id)
78+
secondary_config.append_line(" ip address %s %s" % (secondary_ip, ipv4_addr.netmask))
79+
secondary_config.append_line(" standby version 2")
80+
secondary_config.append_line(" standby 1 ip %s" % virtual_ip)
81+
secondary_config.append_line(" standby 1 priority 254")
82+
secondary_config.append_line(" standby 1 authentication md5 key-string vl%s" % vlan_id)
83+
secondary_config.append_line("!")
84+
85+
# write results
86+
print("Write results...")
87+
primary_config.save_as(os.path.join(output_directory, primary_configuration_file))
88+
secondary_config.save_as(os.path.join(output_directory, secondary_configuration_file))

0 commit comments

Comments
 (0)