|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import logging |
| 4 | +from logging.handlers import TimedRotatingFileHandler |
| 5 | +from netmiko import ConnectHandler, file_transfer |
| 6 | +from pylogrus import PyLogrus, TextFormatter |
| 7 | +import sys |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +def get_logger(log_directory): |
| 13 | + logging.setLoggerClass(PyLogrus) |
| 14 | + logger = logging.getLogger(__name__) # type: PyLogrus |
| 15 | + logger.setLevel(logging.DEBUG) |
| 16 | + |
| 17 | + formatter = TextFormatter(datefmt="Z", colorize=True) |
| 18 | + |
| 19 | + # Console handler |
| 20 | + ch = logging.StreamHandler() |
| 21 | + ch.setLevel(logging.DEBUG) |
| 22 | + ch.setFormatter(formatter) |
| 23 | + logger.addHandler(ch) |
| 24 | + |
| 25 | + # File handler with rolling deletion |
| 26 | + log_filename = os.path.join(log_directory, "backup_restore.log") |
| 27 | + fh = TimedRotatingFileHandler(log_filename, when="midnight", interval=1, backupCount=7) # Keep logs for 7 days |
| 28 | + fh.setLevel(logging.DEBUG) |
| 29 | + fh.setFormatter(formatter) |
| 30 | + logger.addHandler(fh) |
| 31 | + |
| 32 | + return logger |
| 33 | + |
| 34 | + |
| 35 | +def NokiaSrosBackup(ip_address, username, password, config_name, directory, log_directory): |
| 36 | + log = get_logger(log_directory) # Initialize logger |
| 37 | + device = { |
| 38 | + "device_type": "nokia_sros", # Device type is fixed, you shouldn't change this |
| 39 | + "ip": ip_address, # IP address to connect to your device via SSH |
| 40 | + "username": username, # Username for SSH |
| 41 | + "password": password # Password for SSH |
| 42 | + } |
| 43 | + |
| 44 | + # Get the current date and time |
| 45 | + now = datetime.now() |
| 46 | + |
| 47 | + # Format the timestamp |
| 48 | + formatted_now = now.strftime("%Y-%m-%d-%H-%M-%S") |
| 49 | + |
| 50 | + log.info("Device Id :%s", ip_address) |
| 51 | + log.info("Device Config Name :%s", config_name) |
| 52 | + log.info("Device Config Directory :%s", directory) |
| 53 | + |
| 54 | + # join config_name + formatted_now |
| 55 | + config_name = f"{config_name}--{formatted_now}.cfg" |
| 56 | + |
| 57 | + |
| 58 | + # Establish SSH connection to device |
| 59 | + SSH = ConnectHandler(**device) |
| 60 | + if SSH.check_config_mode: |
| 61 | + try: |
| 62 | + log.info("Device is SROS MD-CLI...") |
| 63 | + |
| 64 | + # Send a command and print output |
| 65 | + version = SSH.send_command("show version") |
| 66 | + log.info(version) |
| 67 | + |
| 68 | + log.info(SSH.send_command("environment more false")) |
| 69 | + |
| 70 | + # Get configuration output |
| 71 | + config_output = SSH.send_command("admin show configuration configure") |
| 72 | + log.info (config_output) |
| 73 | + |
| 74 | + # Determine the backup file path |
| 75 | + backup_filename = config_name |
| 76 | + backup_path = os.path.join(directory, backup_filename) |
| 77 | + |
| 78 | + # Save the output to a file with the device name in the specified directory |
| 79 | + with open(backup_path, "w") as file: |
| 80 | + file.write(config_output) |
| 81 | + |
| 82 | + # Revert the environment setting and close SSH connection |
| 83 | + log.info(SSH.send_command("exit")) |
| 84 | + log.info(SSH.send_command("environment more true")) |
| 85 | + |
| 86 | + except Exception as e: |
| 87 | + log.error("An error occurred during config backup: %s", e) |
| 88 | + |
| 89 | + finally: |
| 90 | + log.info(SSH.disconnect()) |
| 91 | + log.info("SSH connection closed") |
| 92 | + else: |
| 93 | + log.info("Device is Classic CLI... Script does not support Classic CLI...") |
| 94 | + |
| 95 | + SSH.disconnect() |
| 96 | + |
| 97 | + |
| 98 | +def NokiaSrosRestore(ip_address, username, password, config_name, directory, log_directory): |
| 99 | + log = get_logger(log_directory) # Initialize logger |
| 100 | + device = { |
| 101 | + "device_type": "nokia_sros", # Device type is fixed, you shouldn't change this |
| 102 | + "ip": ip_address, # IP address to connect to your device via SSH |
| 103 | + "username": username, # Username for SSH |
| 104 | + "password": password # Password for SSH |
| 105 | + } |
| 106 | + |
| 107 | + log.info("Device Id :%s", ip_address) |
| 108 | + log.info("Device Config Name :%s.cfg", config_name) |
| 109 | + log.info("Device Config Directory :%s", directory) |
| 110 | + |
| 111 | + SSH = ConnectHandler(**device) |
| 112 | + if SSH.check_config_mode: |
| 113 | + log.info("Device is SROS MD-CLI...") |
| 114 | + try: |
| 115 | + log.info("SSH connection established to device: %s", ip_address) |
| 116 | + |
| 117 | + # Determine the restore file path |
| 118 | + restore_filename = f"{config_name}" |
| 119 | + restore_path = os.path.join(directory, restore_filename) |
| 120 | + |
| 121 | + transfer = file_transfer( |
| 122 | + SSH, |
| 123 | + source_file=restore_path, |
| 124 | + dest_file=config_name, |
| 125 | + file_system="cf3:/", |
| 126 | + direction="put", |
| 127 | + overwrite_file=True |
| 128 | + ) |
| 129 | + log.info("File transferred to device: %s", transfer) |
| 130 | + |
| 131 | + log.info(SSH.send_command("configure private")) |
| 132 | + log.info(SSH.send_command(f"load full-replace cf3:{config_name}")) |
| 133 | + log.info(SSH.send_command("commit")) |
| 134 | + |
| 135 | + except Exception as e: |
| 136 | + log.error("An error occurred during SCP: %s", e) |
| 137 | + |
| 138 | + finally: |
| 139 | + log.info(SSH.disconnect()) |
| 140 | + log.info("SSH connection closed") |
| 141 | + |
| 142 | +def NokiaSrosGet(ip_address, username, password, config_name, directory, log_directory): |
| 143 | + log = get_logger(log_directory) # Initialize logger |
| 144 | + device = { |
| 145 | + "device_type": "nokia_sros", # Device type is fixed, you shouldn't change this |
| 146 | + "ip": ip_address, # IP address to connect to your device via SSH |
| 147 | + "username": username, # Username for SSH |
| 148 | + "password": password # Password for SSH |
| 149 | + } |
| 150 | + |
| 151 | + config_name = f"{config_name}-running.cfg" |
| 152 | + |
| 153 | + log.info("Device Id :%s", ip_address) |
| 154 | + log.info("Device Config Name :%s", config_name) |
| 155 | + log.info("Device Config Directory :%s", directory) |
| 156 | + |
| 157 | + |
| 158 | + # Establish SSH connection to device |
| 159 | + SSH = ConnectHandler(**device) |
| 160 | + if SSH.check_config_mode: |
| 161 | + try: |
| 162 | + log.info("Device is SROS MD-CLI...") |
| 163 | + |
| 164 | + # Send a command and print output |
| 165 | + version = SSH.send_command("show version") |
| 166 | + log.info(version) |
| 167 | + |
| 168 | + log.info(SSH.send_command("environment more false")) |
| 169 | + |
| 170 | + # Get configuration output |
| 171 | + config_output = SSH.send_command("admin show configuration configure") |
| 172 | + log.info (config_output) |
| 173 | + |
| 174 | + # Determine the backup file path |
| 175 | + backup_filename = config_name |
| 176 | + backup_path = os.path.join(directory, backup_filename) |
| 177 | + |
| 178 | + # Save the output to a file with the device name in the specified directory |
| 179 | + with open(backup_path, "w") as file: |
| 180 | + file.write(config_output) |
| 181 | + |
| 182 | + # Revert the environment setting and close SSH connection |
| 183 | + log.info(SSH.send_command("exit")) |
| 184 | + log.info(SSH.send_command("environment more true")) |
| 185 | + |
| 186 | + except Exception as e: |
| 187 | + log.error("An error occurred during config backup: %s", e) |
| 188 | + |
| 189 | + finally: |
| 190 | + log.info(SSH.disconnect()) |
| 191 | + log.info("SSH connection closed") |
| 192 | + else: |
| 193 | + log.info("Device is Classic CLI... Script does not support Classic CLI...") |
| 194 | + |
| 195 | + SSH.disconnect() |
| 196 | + |
| 197 | +def main(): |
| 198 | + parser = argparse.ArgumentParser( |
| 199 | + description="Backup and Restore device configuration.", |
| 200 | + formatter_class=argparse.RawTextHelpFormatter, |
| 201 | + usage="""backupRestoreScript.py --ip_address IPADDRESS --username USERNAME --password PASSWORD --configname CONFIGNAME --kind KIND --directory DIRECTORY --log_directory LOG_DIRECTORY [--backup] [--restore] |
| 202 | +
|
| 203 | +Examples: |
| 204 | + python3 backupRestoreScript.py --ip_address 10.2.1.110 --username admin --password admin --configname Router10-2024-06-26T08:46:41Z.cfg --kind vr-sros --directory /path/to/backup --log_directory /path/to/logs --backup |
| 205 | + python3 backupRestoreScript.py --ip_address 10.2.1.110 --username admin --password admin --configname Router10-2024-06-26T08:46:41Z.cfg --kind vr-sros --directory /path/to/backup --log_directory /path/to/logs --restore |
| 206 | +""" |
| 207 | + ) |
| 208 | + |
| 209 | + parser.add_argument("--ip_address", required=True, help="IP address of the device") |
| 210 | + parser.add_argument("--username", required=True, help="Username for SSH login") |
| 211 | + parser.add_argument("--password", required=True, help="Password for SSH login") |
| 212 | + parser.add_argument("--configname", required=True, help="Name of the device config for backup/restore file naming") |
| 213 | + parser.add_argument("--directory", "-d", required=True, help="Directory where backup/restore files should be saved") |
| 214 | + parser.add_argument("--kind", required=True, help="Device kind ie: sros") |
| 215 | + parser.add_argument("--log_directory", required=True, help="Directory where logs should be saved") |
| 216 | + parser.add_argument("--backup", action="store_true", help="Backup the device configuration") |
| 217 | + parser.add_argument("--restore", action="store_true", help="Restore the configuration") |
| 218 | + parser.add_argument("--get", action="store_true", help="Get the running configuration") |
| 219 | + |
| 220 | + |
| 221 | + args = parser.parse_args() |
| 222 | + |
| 223 | + if not any(vars(args).values()): |
| 224 | + parser.print_help() |
| 225 | + elif args.backup: |
| 226 | + if args.kind == "vr-sros": |
| 227 | + NokiaSrosBackup(args.ip_address, args.username, args.password, args.configname, args.directory, args.log_directory) |
| 228 | + elif args.restore: |
| 229 | + if args.kind == "vr-sros": |
| 230 | + NokiaSrosRestore(args.ip_address, args.username, args.password, args.configname, args.directory, args.log_directory) |
| 231 | + elif args.get: |
| 232 | + if args.kind == "vr-sros": |
| 233 | + NokiaSrosGet(args.ip_address, args.username, args.password, args.configname, args.directory, args.log_directory) |
| 234 | + else: |
| 235 | + parser.print_help() |
| 236 | + |
| 237 | + |
| 238 | +if __name__ == "__main__": |
| 239 | + main() |
0 commit comments