def execute_command(self, command): """Execute command on ASA and return output""" try: stdin, stdout, stderr = self.ssh_client.exec_command(command) output = stdout.read().decode('utf-8') error = stderr.read().decode('utf-8') if error: self.logger.warning(f"Command error: {error}") return output except Exception as e: self.logger.error(f"Command execution failed: {str(e)}") return None
def download_crypto_keys(self, destination_path): """Download crypto keys and certificates""" self.logger.info("Exporting crypto information...") crypto_data = [] commands = [ "show crypto key mypubkey rsa", "show crypto ca certificates", "show crypto ca trustpool" ] for cmd in commands: output = self.execute_command(cmd) if output: crypto_data.append(f"\n{'='*60}\nCommand: {cmd}\n{'='*60}\n") crypto_data.append(output) if crypto_data: filename = os.path.join(destination_path, f"crypto_info_{self.hostname}.txt") with open(filename, 'w') as f: f.writelines(crypto_data) self.logger.info(f"Crypto info saved to: {filename}") return filename return None
def disconnect(self): """Close SSH connection""" if self.ssh_client: self.ssh_client.close() self.logger.info("SSH connection closed") def main(): parser = argparse.ArgumentParser(description='Cisco ASA 5506-X Download Utility') parser.add_argument('--host', required=True, help='ASA hostname or IP address') parser.add_argument('--username', required=True, help='SSH username') parser.add_argument('--password', required=True, help='SSH password') parser.add_argument('--port', type=int, default=22, help='SSH port (default: 22)') parser.add_argument('--output', default='./asa_backups', help='Output directory')
class CiscoASADownloader: def (self, hostname, username, password, port=22): self.hostname = hostname self.username = username self.password = password self.port = port self.ssh_client = None self.setup_logging() cisco asa 5506-x download
#!/usr/bin/env python3 """ Cisco ASA 5506-X File Download Utility Supports: Running config, Startup config, ASDM image, AnyConnect packages """ import paramiko import os import sys import logging from scp import SCPClient import argparse from datetime import datetime
def connect(self): """Establish SSH connection to ASA""" try: self.ssh_client = paramiko.SSHClient() self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.logger.info(f"Connecting to {self.hostname}...") self.ssh_client.connect( hostname=self.hostname, port=self.port, username=self.username, password=self.password, timeout=30, allow_agent=False, look_for_keys=False ) self.logger.info("SSH connection established") return True except Exception as e: self.logger.error(f"Connection failed: {str(e)}") return False
# Initialize downloader downloader = CiscoASADownloader(args.host, args.username, args.password, args.port) "show crypto ca certificates"
This feature provides secure, automated backup capabilities for your Cisco ASA 5506-X with comprehensive logging and error handling.
def download_asdm_image(self, destination_path): """Download ASDM image from flash""" # First, find ASDM file in flash flash_contents = self.execute_command("show flash:") if flash_contents: # Parse for .bin files containing 'asdm' for line in flash_contents.split('\n'): if 'asdm' in line.lower() and '.bin' in line: filename = line.split()[-1] if line.split() else None if filename: remote_path = f"/{filename}" local_path = os.path.join(destination_path, filename) return self.download_file_via_scp(remote_path, local_path) self.logger.warning("ASDM image not found in flash") return False
def list_flash_files(self): """List files in flash memory""" self.logger.info("Listing flash files...") output = self.execute_command("show flash:") if output: print("\nFlash Files:") print("=" * 60) print(output) return output return None f"crypto_info_{self.hostname}.txt") with open(filename
# Action arguments parser.add_argument('--backup-all', action='store_true', help='Complete backup') parser.add_argument('--running-config', action='store_true', help='Download running config only') parser.add_argument('--startup-config', action='store_true', help='Download startup config only') parser.add_argument('--list-flash', action='store_true', help='List flash files') parser.add_argument('--download-asdm', action='store_true', help='Download ASDM image') parser.add_argument('--download-file', help='Download specific file from flash')
def download_startup_config(self, destination_path): """Download startup configuration""" self.logger.info("Downloading startup configuration...") config = self.execute_command("show startup-config") if config: filename = os.path.join(destination_path, f"startup_config_{self.hostname}.cfg") with open(filename, 'w') as f: f.write(config) self.logger.info(f"Startup config saved to: {filename}") return filename return None