pretty backups of a single switch
This commit is contained in:
parent
e5b989b367
commit
056b5e8df0
3 changed files with 91 additions and 1 deletions
24
backup.py
Normal file
24
backup.py
Normal file
|
@ -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)
|
2
core.py
2
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):
|
||||
|
|
66
pretty.py
Normal file
66
pretty.py
Normal file
|
@ -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"))
|
Loading…
Reference in a new issue