From 056b5e8df0ebd582f0b251f9c2d0f85391fbf0d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Paulon?= Date: Thu, 17 Sep 2020 14:41:43 +0200 Subject: [PATCH] pretty backups of a single switch --- backup.py | 24 ++++++++++++++++++++ core.py | 2 +- pretty.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 backup.py create mode 100644 pretty.py diff --git a/backup.py b/backup.py new file mode 100644 index 0000000..90f05cc --- /dev/null +++ b/backup.py @@ -0,0 +1,24 @@ +import argparse + +from pathlib import Path + +from core import connect_to_switch, sftp_read_file +from pretty import beautify_conf + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Script de backup de la configuration des switchs") + parser.add_argument("switch_name", type=str, help="Sauvegarde la configuration de ce switch") + parser.add_argument("-n", "--no-pretty", action="store_true", help="N'affiche pas la version pretty") + parser.add_argument("-c", "--case-size",type=int, help="Taille de la case", default=10) + args = parser.parse_args() + + + + print("Backing up {}".format(args.switch_name)) + session = connect_to_switch("{}.switches.crans.org".format(args.switch_name), key="/root/.ssh/id_rsa") + config = sftp_read_file(session, "cfg/running-config") + if not args.no_pretty: + beautify_conf(config.decode("utf-8").split("\n"), case_size=args.case_size) + Path("configs-backup").mkdir(exist_ok=True) + with open("configs-backup/{}.bak".format(args.switch_name), "wb") as config_bak: + config_bak.write(config) diff --git a/core.py b/core.py index 4ccd1d5..7f4a432 100644 --- a/core.py +++ b/core.py @@ -55,7 +55,7 @@ def connect_to_switch(address, port=22, user="root", password=None, key=None): s.connect(sockaddr) session = Session() session.handshake(s) - # Authentication + # Authentication if key is not None: logger.debug("Connecting with private key {}".format(key)) if os.path.isfile(key): diff --git a/pretty.py b/pretty.py new file mode 100644 index 0000000..0daf8c8 --- /dev/null +++ b/pretty.py @@ -0,0 +1,66 @@ +def pprint_interfaces(i_dict, case_size=10): + for module, inter in i_dict.items(): + lines = ["", "", "", ""] + if module != "": + print("Module " + module) + for k, v in inter.items(): + lines[0] += "-" * case_size + "|" + lines[1] += str(k) + " " * (case_size - len(str(k))) + "|" + name = v.get("name", "") + if len(name) > case_size: + name = name[:case_size] + lines[2] += name + " " * (case_size - len(name)) + "|" + lines[3] += "-" * case_size + "|" + last_i = 0 + step = 100//(case_size + 1) * (case_size + 1) + first_row = True + if len(lines[0]) <= step: + for line in lines: + print("|" + line) + else: + for i in range(step, len(lines[0]), step): + for line in lines: + if first_row: + print(" |" + line[last_i:i] + "...") + else: + print("..." + line[last_i-1:i] + "...") + first_row = False + last_i = i + print() + for line in lines: + if line != "": + print("..." + line[last_i-1:]) + + +def beautify_conf(config_file, case_size=10): + vlan_dict = dict() + interfaces_dict = dict() + switch_infos = dict() + current_interface = None + current_module = None + for line in config_file: + if line.startswith("hostname"): + switch_infos["hostname"] = line.split(" ")[1].strip("\"") + if line.startswith("interface"): + try: + current_module = "" + current_interface = int(line.split(" ")[1]) + except ValueError: + current_module = line.split(" ")[1][0] + current_interface = int(line.split(" ")[1][1:]) + if interfaces_dict.get(current_module) is None: + interfaces_dict[current_module] = dict() + if interfaces_dict[current_module].get(current_interface) is None: + interfaces_dict[current_module][current_interface] = dict() + if line.strip().startswith("name") and current_interface is not None: + interfaces_dict[current_module][current_interface]["name"] = " ".join(line.strip().split(" ")[1:]).strip("\"") + if line.strip().startswith("exit"): + if current_interface is not None: + current_interface = None + if current_module is not None: + current_module = None + + pprint_interfaces(interfaces_dict, case_size) + +if __name__ == "__main__": + beautify_conf(open("configs-backup/bata-0.bak", "r"))