From ae241b481285f4556d87f2fe221779814e72521e Mon Sep 17 00:00:00 2001 From: stahlz Date: Tue, 30 Sep 2025 18:11:48 +0000 Subject: [PATCH] Add unifi_reboot.py --- unifi_reboot.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 unifi_reboot.py diff --git a/unifi_reboot.py b/unifi_reboot.py new file mode 100644 index 0000000..20ae34c --- /dev/null +++ b/unifi_reboot.py @@ -0,0 +1,43 @@ +#!/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() \ No newline at end of file