|
| 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