Skip to content

Instantly share code, notes, and snippets.

@ccrsxx
Created October 12, 2025 12:11
Show Gist options
  • Select an option

  • Save ccrsxx/a2bb3a465f0ba172b533de6599a08411 to your computer and use it in GitHub Desktop.

Select an option

Save ccrsxx/a2bb3a465f0ba172b533de6599a08411 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Unified UniFi LED controller
# Usage: unifi_led.sh <on|off>
# ==== Configuration ====
SSH_KEY="/path/to/private.key"
SSH_USER="ccrsxx"
# List of AP IPs
APS=(
"192.168.20.100"
# Add more APs here...
)
# Path to config file on each AP
CFG="/var/etc/persistent/cfg/mgmt"
# =======================
STATE="$1"
if [[ -z "$STATE" ]]; then
echo "Usage: $0 <on|off>"
exit 1
fi
if [[ "$STATE" != "on" && "$STATE" != "off" ]]; then
echo "Invalid state: $STATE (must be 'on' or 'off')"
exit 1
fi
# Define replacements based on desired state
if [[ "$STATE" == "on" ]]; then
FROM="mgmt.led_enabled=false"
TO="mgmt.led_enabled=true"
MSG="Turning LED ON"
else
FROM="mgmt.led_enabled=true"
TO="mgmt.led_enabled=false"
MSG="Turning LED OFF"
fi
timestamp() { date '+%Y-%m-%d %H:%M:%S'; }
echo "[$(timestamp)] $MSG"
for AP_IP in "${APS[@]}"; do
echo "[$(timestamp)] AP $AP_IP"
ssh -i "$SSH_KEY" -o ConnectTimeout=5 -o StrictHostKeyChecking=no "${SSH_USER}@${AP_IP}" sh -s <<EOF
if [ -f "$CFG" ]; then
if grep -q "$FROM" "$CFG"; then
sed -i "s/$FROM/$TO/" "$CFG"
echo "[$(timestamp)] LED state changed to $STATE"
else
echo "[$(timestamp)] LED already $STATE"
fi
else
echo "Error: config file not found ($CFG)"
fi
EOF
done
echo "[$(timestamp)] Completed LED $STATE action"
@ccrsxx
Copy link
Author

ccrsxx commented Oct 12, 2025

crontab

# Turn LED off at 23:00 every day
0 23 * * * /usr/local/bin/unifi-led.sh off

# Turn LED on at 06:00 every day
0 6 * * * /usr/local/bin/unifi-led.sh on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment