Public Access
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
import paramiko
|
|
import datetime
|
|
|
|
# Device connection info
|
|
DEVICE_IP = "192.168.100.254"
|
|
USERNAME = "stahlz"
|
|
PASSWORD = "ZlNOBgEsbZ6ISXj0"
|
|
REBOOT_CMD = "sudo /sbin/reboot"
|
|
LOGFILE = "unifi-reboot.log"
|
|
|
|
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():
|
|
log(f"Rebooting {DEVICE_IP} (user: {USERNAME})...")
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
ssh.connect(DEVICE_IP, username=USERNAME, password=PASSWORD, timeout=10)
|
|
stdin, stdout, stderr = ssh.exec_command(REBOOT_CMD)
|
|
output = stdout.read().decode().strip()
|
|
ssh.close()
|
|
log(f"SUCCESS: {DEVICE_IP} reboot command sent.")
|
|
log(f"Output: {output}")
|
|
except Exception as e:
|
|
log(f"ERROR: Failed to reboot {DEVICE_IP}. Reason: {e}")
|
|
log(f"Error: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
reboot_device() |