#!/usr/bin/env python3 import paramiko import datetime import sys # File with device list: "ip user password" DEVICE_LIST = "/usr/local/etc/unifi-devices.txt" LOGFILE = "/var/log/unifi-reboot.log" REBOOT_CMD = "/sbin/reboot" def log(message): timestamp = datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S]") with open(LOGFILE, "a") as f: f.write(f"{timestamp} {message}\n") print(f"{timestamp} {message}") def reboot_device(ip, user, password): log(f"Rebooting {ip} (user: {user})...") try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip, username=user, password=password, timeout=10) ssh.exec_command(REBOOT_CMD) ssh.close() log(f"SUCCESS: {ip} reboot command sent.") except Exception as e: log(f"ERROR: Failed to reboot {ip}. Reason: {e}") def main(): try: with open(DEVICE_LIST) as f: for line in f: parts = line.strip().split() if len(parts) != 3: continue # skip malformed lines ip, user, password = parts reboot_device(ip, user, password) except FileNotFoundError: log(f"ERROR: Device list file not found: {DEVICE_LIST}") sys.exit(1) if __name__ == "__main__": main()