From cb97a4f4123d6d585863a47a14fb7b153c771830 Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 4 Oct 2020 13:33:11 +0200 Subject: [PATCH] Correction de bug + ajout module --- configs/config.j2 | 19 +++++++++++-------- generate-conf.py | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/configs/config.j2 b/configs/config.j2 index 61d9d94..66d2aa0 100644 --- a/configs/config.j2 +++ b/configs/config.j2 @@ -1,5 +1,8 @@ {{ header }} hostname "{{ hostname }}" +{%- for module in modules %} +module {{ module }} +{%- endfor %} console idle-timeout 1800 console idle-timeout serial-usb 1800 no cdp run @@ -111,7 +114,7 @@ vlan {{ number }} {%- endif %} exit {%- endfor %} -allow-unsupported-transceiver +; allow-unsupported-transceiver {%- if loop_protect %} loop-protect {{ loop_protect.ports }} loop-protect transmit-interval 3 disable-timer 30 @@ -121,10 +124,10 @@ arp-protect arp-protect validate src-mac dest-mac arp-protect vlan {{ arp_protect.vlans }} {%- endif %} -device-profile name "default-ap-profile" - cos 0 - exit -activate software-update disable -activate provision disable -password manager -password operator +; device-profile name "default-ap-profile" +; cos 0 +; exit +; activate software-update disable +; activate provision disable +; password manager +; password operator diff --git a/generate-conf.py b/generate-conf.py index b006e37..992e2b6 100644 --- a/generate-conf.py +++ b/generate-conf.py @@ -146,16 +146,24 @@ def get_header(old_config): header = "\n".join(old_config.split("\n")[:2]) return header +def get_modules(old_config): + modules = list() + for line in old_config.split("\n"): + if line.startswith("module"): + modules.append(" ".join(line.split(" ")[1:])) + return modules + def conf_from_dict(config_dict): with open("configs/config.j2", "r") as template_file: template = Template(template_file.read()) configuration = template.render(config_dict) return configuration -def gen_conf(master_config, switch_config, header): +def gen_conf(master_config, switch_config, header, modules): interfaces, vlans, mac_based_ports, ra_guard_ports, dhcp_snooping_vlans = gen_interfaces(switch_config) config_dict = { "header": header, + "modules": modules, "hostname": switch_config.get("hostname"), "dhcp_servers": master_config.get("dhcp_servers"), "dhcpv6_servers": master_config.get("dhcpv6_servers"), @@ -175,11 +183,16 @@ def gen_conf(master_config, switch_config, header): } return conf_from_dict(config_dict) -def gen_conf_re2o(re2o_config, header): +def gen_conf_re2o(re2o_config, header, modules): mgmt_utils = re2o_config.get("switchs_management_utils") ipv4_managers = dict() - for m in mgmt_utils.get("subnet"): - ipv4_managers[m.get("network")] = { "ip": m.get("network"), "subnet": m.get("netmask")} + subnets = mgmt_utils.get("subnet") + if isinstance(subnets, list): + for m in mgmt_utils.get("subnet"): + print(m) + ipv4_managers[m.get("network")] = { "ip": m.get("network"), "subnet": m.get("netmask")} + else: + ipv4_managers[subnets.get("network")] = { "ip": subnets.get("network"), "subnet": subnets.get("netmask")} ipv6_managers = dict() # FUCK YOU ! subnet6 c'est pas une liste de subnets mais un seul subnet m = mgmt_utils.get("subnet6") @@ -208,12 +221,19 @@ def gen_conf_re2o(re2o_config, header): # on rajoute les ips sur les vlans où il y en a for address, iface in re2o_config.get("interfaces_subnet", dict()).items(): # ouais y'a une autre liste là, don't ask - for i in iface: - if i["vlan_id"] == vlan_id: + if isinstance(iface, list): + for i in iface: + if i["vlan_id"] == vlan_id: + if vlans[vlan_id].get("ip") is None: + vlans[vlan_id]["ip"] = dict() + vlans[vlan_id]["ip"]["addr"] = address + vlans[vlan_id]["ip"]["subnet"] = i["netmask"] + else: + if iface["vlan_id"] == vlan_id: if vlans[vlan_id].get("ip") is None: vlans[vlan_id]["ip"] = dict() vlans[vlan_id]["ip"]["addr"] = address - vlans[vlan_id]["ip"]["subnet"] = i["netmask"] + vlans[vlan_id]["ip"]["subnet"] = iface["netmask"] for address, iface in re2o_config.get("interfaces6_subnet", dict()).items(): if iface["vlan_id"] == vlan_id: if vlans[vlan_id].get("ip") is None: @@ -260,6 +280,7 @@ def gen_conf_re2o(re2o_config, header): radius_servers = [ {"ip": i, "secret": radius_key } for i in mgmt_utils["radius_servers"]["ipv4"] + mgmt_utils["radius_servers"]["ipv6"]] config_dict = { "header": header, + "modules": modules, "location": re2o_config.get("switchbay").get("name"), "hostname": re2o_config.get("short_name"), "dhcp_servers": mgmt_utils.get("dhcp_servers").get("ipv4"), @@ -376,13 +397,14 @@ if __name__ == "__main__": session = connect_to_switch(switch_address, user="root", key=master_config.get("ssh_private_key")) #TODO: spécifier chemin clef old_config = sftp_read_file(session, "cfg/running-config").decode("utf-8") header = get_header(old_config) + modules = get_modules(old_config) # génération de la conf logging.info("Generating configuration for {}".format(args.switch_name)) if args.re2o: - configuration = gen_conf_re2o(re2o_config, header) + configuration = gen_conf_re2o(re2o_config, header, modules) else: - configuration = gen_conf(master_config, switch_config, header) + configuration = gen_conf(master_config, switch_config, header, modules) # génération du diff for line in difflib.unified_diff(old_config.split("\n"), configuration.split("\n"), fromfile='origin', tofile='new', lineterm=""):