import pexpect import argparse import os import tempfile def backup_wlc(ip, username, password,filename): print(f"Backing up WLC {ip}") print(f"Logging in with {username} and {password}") child = pexpect.spawn(f"ssh -oHostKeyAlgorithms=+ssh-ed25519 {username}@{ip}") child.expect("password:") child.sendline(password) child.expect("#") prompt = child.before.lstrip().decode("utf-8") + "#" prompt = prompt.replace("(","\(") prompt = prompt.replace(")","\)") prompt = prompt.replace("*","\*") prompt = prompt.replace("[","\[") prompt = prompt.replace("]","\]") child.sendline("no paging") child.expect(prompt) child.sendline("show running") child.expect(prompt) #print(child.before) running_config=child.before.decode("utf-8") running_config = running_config.replace("\r\n","\n") # Write to a temporary file with tempfile.NamedTemporaryFile(delete=False, mode="w", encoding="utf-8",dir=os.path.dirname(filename)) as temp_file: temp_file.write(running_config) temp_file_path = temp_file.name # Rename the temporary file to the desired output file try: os.rename(temp_file_path, filename) print(f"Backup saved to {filename}") except Exception as e: print(f"Failed to save backup: {e}") os.remove(temp_file_path) # Clean up the temporary file if __name__ == "__main__": parser = argparse.ArgumentParser(description="backup aruba wlc") parser.add_argument("--username", help="Username for login", required=True) parser.add_argument("--password", help="Password for login", required=True) parser.add_argument("--ip", help="IP address of WLC", required=True) parser.add_argument("--filename", help="filename to save file", required=True) args = parser.parse_args() backup_wlc(args.ip, args.username, args.password,args.filename)