Correction de bug + ajout module
This commit is contained in:
parent
eed53de31a
commit
cb97a4f412
2 changed files with 42 additions and 17 deletions
|
@ -1,5 +1,8 @@
|
||||||
{{ header }}
|
{{ header }}
|
||||||
hostname "{{ hostname }}"
|
hostname "{{ hostname }}"
|
||||||
|
{%- for module in modules %}
|
||||||
|
module {{ module }}
|
||||||
|
{%- endfor %}
|
||||||
console idle-timeout 1800
|
console idle-timeout 1800
|
||||||
console idle-timeout serial-usb 1800
|
console idle-timeout serial-usb 1800
|
||||||
no cdp run
|
no cdp run
|
||||||
|
@ -111,7 +114,7 @@ vlan {{ number }}
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
exit
|
exit
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
allow-unsupported-transceiver
|
; allow-unsupported-transceiver
|
||||||
{%- if loop_protect %}
|
{%- if loop_protect %}
|
||||||
loop-protect {{ loop_protect.ports }}
|
loop-protect {{ loop_protect.ports }}
|
||||||
loop-protect transmit-interval 3 disable-timer 30
|
loop-protect transmit-interval 3 disable-timer 30
|
||||||
|
@ -121,10 +124,10 @@ arp-protect
|
||||||
arp-protect validate src-mac dest-mac
|
arp-protect validate src-mac dest-mac
|
||||||
arp-protect vlan {{ arp_protect.vlans }}
|
arp-protect vlan {{ arp_protect.vlans }}
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
device-profile name "default-ap-profile"
|
; device-profile name "default-ap-profile"
|
||||||
cos 0
|
; cos 0
|
||||||
exit
|
; exit
|
||||||
activate software-update disable
|
; activate software-update disable
|
||||||
activate provision disable
|
; activate provision disable
|
||||||
password manager
|
; password manager
|
||||||
password operator
|
; password operator
|
||||||
|
|
|
@ -146,16 +146,24 @@ def get_header(old_config):
|
||||||
header = "\n".join(old_config.split("\n")[:2])
|
header = "\n".join(old_config.split("\n")[:2])
|
||||||
return header
|
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):
|
def conf_from_dict(config_dict):
|
||||||
with open("configs/config.j2", "r") as template_file:
|
with open("configs/config.j2", "r") as template_file:
|
||||||
template = Template(template_file.read())
|
template = Template(template_file.read())
|
||||||
configuration = template.render(config_dict)
|
configuration = template.render(config_dict)
|
||||||
return configuration
|
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)
|
interfaces, vlans, mac_based_ports, ra_guard_ports, dhcp_snooping_vlans = gen_interfaces(switch_config)
|
||||||
config_dict = {
|
config_dict = {
|
||||||
"header": header,
|
"header": header,
|
||||||
|
"modules": modules,
|
||||||
"hostname": switch_config.get("hostname"),
|
"hostname": switch_config.get("hostname"),
|
||||||
"dhcp_servers": master_config.get("dhcp_servers"),
|
"dhcp_servers": master_config.get("dhcp_servers"),
|
||||||
"dhcpv6_servers": master_config.get("dhcpv6_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)
|
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")
|
mgmt_utils = re2o_config.get("switchs_management_utils")
|
||||||
ipv4_managers = dict()
|
ipv4_managers = dict()
|
||||||
for m in mgmt_utils.get("subnet"):
|
subnets = mgmt_utils.get("subnet")
|
||||||
ipv4_managers[m.get("network")] = { "ip": m.get("network"), "subnet": m.get("netmask")}
|
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()
|
ipv6_managers = dict()
|
||||||
# FUCK YOU ! subnet6 c'est pas une liste de subnets mais un seul subnet
|
# FUCK YOU ! subnet6 c'est pas une liste de subnets mais un seul subnet
|
||||||
m = mgmt_utils.get("subnet6")
|
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
|
# on rajoute les ips sur les vlans où il y en a
|
||||||
for address, iface in re2o_config.get("interfaces_subnet", dict()).items():
|
for address, iface in re2o_config.get("interfaces_subnet", dict()).items():
|
||||||
# ouais y'a une autre liste là, don't ask
|
# ouais y'a une autre liste là, don't ask
|
||||||
for i in iface:
|
if isinstance(iface, list):
|
||||||
if i["vlan_id"] == vlan_id:
|
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:
|
if vlans[vlan_id].get("ip") is None:
|
||||||
vlans[vlan_id]["ip"] = dict()
|
vlans[vlan_id]["ip"] = dict()
|
||||||
vlans[vlan_id]["ip"]["addr"] = address
|
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():
|
for address, iface in re2o_config.get("interfaces6_subnet", dict()).items():
|
||||||
if iface["vlan_id"] == vlan_id:
|
if iface["vlan_id"] == vlan_id:
|
||||||
if vlans[vlan_id].get("ip") is None:
|
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"]]
|
radius_servers = [ {"ip": i, "secret": radius_key } for i in mgmt_utils["radius_servers"]["ipv4"] + mgmt_utils["radius_servers"]["ipv6"]]
|
||||||
config_dict = {
|
config_dict = {
|
||||||
"header": header,
|
"header": header,
|
||||||
|
"modules": modules,
|
||||||
"location": re2o_config.get("switchbay").get("name"),
|
"location": re2o_config.get("switchbay").get("name"),
|
||||||
"hostname": re2o_config.get("short_name"),
|
"hostname": re2o_config.get("short_name"),
|
||||||
"dhcp_servers": mgmt_utils.get("dhcp_servers").get("ipv4"),
|
"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
|
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")
|
old_config = sftp_read_file(session, "cfg/running-config").decode("utf-8")
|
||||||
header = get_header(old_config)
|
header = get_header(old_config)
|
||||||
|
modules = get_modules(old_config)
|
||||||
|
|
||||||
# génération de la conf
|
# génération de la conf
|
||||||
logging.info("Generating configuration for {}".format(args.switch_name))
|
logging.info("Generating configuration for {}".format(args.switch_name))
|
||||||
if args.re2o:
|
if args.re2o:
|
||||||
configuration = gen_conf_re2o(re2o_config, header)
|
configuration = gen_conf_re2o(re2o_config, header, modules)
|
||||||
else:
|
else:
|
||||||
configuration = gen_conf(master_config, switch_config, header)
|
configuration = gen_conf(master_config, switch_config, header, modules)
|
||||||
|
|
||||||
# génération du diff
|
# génération du diff
|
||||||
for line in difflib.unified_diff(old_config.split("\n"), configuration.split("\n"), fromfile='origin', tofile='new', lineterm=""):
|
for line in difflib.unified_diff(old_config.split("\n"), configuration.split("\n"), fromfile='origin', tofile='new', lineterm=""):
|
||||||
|
|
Loading…
Reference in a new issue