From 144031bdf6851152989b42b12c74d2ef1ebcad74 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sat, 5 Jul 2025 23:23:57 +0000 Subject: [PATCH] Move to config file --- config.yaml | 34 ++++++++++++++++++++ router_backup.py | 84 ++++++++++++++++++++++++++++++------------------ 2 files changed, 87 insertions(+), 31 deletions(-) create mode 100644 config.yaml diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..1f22416 --- /dev/null +++ b/config.yaml @@ -0,0 +1,34 @@ +types: + srlinux: + commands: + - show version + - show platform fan-tray + - show platform linecard + - show platform power-supply + - info flat from running + +devices: + asw100: + user: pim + commands: + - show version + - show platform fan-tray + - show platform linecard + - show platform power-supply + - info flat from running + asw120: + user: pim + commands: + - show version + - show platform fan-tray + - show platform linecard + - show platform power-supply + - info flat from running + asw121: + user: pim + commands: + - show version + - show platform fan-tray + - show platform linecard + - show platform power-supply + - info flat from running diff --git a/router_backup.py b/router_backup.py index 2c08e25..f869924 100755 --- a/router_backup.py +++ b/router_backup.py @@ -10,6 +10,7 @@ import os from datetime import datetime import argparse import json +import yaml class RouterBackup: @@ -136,26 +137,21 @@ class RouterBackup: def main(): parser = argparse.ArgumentParser(description='SSH Router Backup Tool') - parser.add_argument('--host', required=True, help='Router hostname or IP address') - parser.add_argument('--user', required=True, help='SSH username') + parser.add_argument('--config', required=True, help='YAML configuration file path') parser.add_argument('--password', help='SSH password') parser.add_argument('--key-file', help='SSH private key file path') parser.add_argument('--port', type=int, default=22, help='SSH port (default: 22)') parser.add_argument('--output-dir', default='/tmp', help='Output directory for command output files (default: /tmp)') - parser.add_argument('--commands', nargs='+', help='Commands to run on the router') args = parser.parse_args() - # Default commands for SR Linux routers - default_commands = [ - "show version", - "show system information", - "show interface", - "show network-instance", - "show route-table" - ] - - commands = args.commands if args.commands else default_commands + # Load configuration + try: + with open(args.config, 'r') as f: + config = yaml.safe_load(f) + except Exception as e: + print(f"Failed to load config file {args.config}: {e}") + sys.exit(1) # Use SSH key by default, fall back to password if not args.password and not args.key_file: @@ -181,25 +177,51 @@ def main(): import getpass args.password = getpass.getpass("No SSH key found. Enter SSH password: ") - # Create backup instance - backup = RouterBackup( - hostname=args.host, - username=args.user, - password=args.password, - key_file=args.key_file, - port=args.port - ) - - # Connect and backup - if backup.connect(): - try: - backup_info = backup.backup_commands(commands, args.output_dir) - print(f"\nBackup completed. Files saved to '{args.output_dir}' directory") - print(f"Summary: {sum(1 for cmd in backup_info['commands'].values() if cmd['success'])}/{len(commands)} commands successful") - finally: - backup.disconnect() - else: + # Process each device in config + devices = config.get('devices', {}) + if not devices: + print("No devices found in config file") sys.exit(1) + + success_count = 0 + total_count = len(devices) + + for hostname, device_config in devices.items(): + print(f"\nProcessing device: {hostname}") + + user = device_config.get('user') + commands = device_config.get('commands', []) + + if not user: + print(f"No user specified for {hostname}, skipping") + continue + + if not commands: + print(f"No commands specified for {hostname}, skipping") + continue + + # Create backup instance + backup = RouterBackup( + hostname=hostname, + username=user, + password=args.password, + key_file=args.key_file, + port=args.port + ) + + # Connect and backup + if backup.connect(): + try: + backup_info = backup.backup_commands(commands, args.output_dir) + print(f"Backup completed for {hostname}") + print(f"Summary: {sum(1 for cmd in backup_info['commands'].values() if cmd['success'])}/{len(commands)} commands successful") + success_count += 1 + finally: + backup.disconnect() + else: + print(f"Failed to connect to {hostname}") + + print(f"\nOverall summary: {success_count}/{total_count} devices processed successfully") if __name__ == "__main__":