#!/bin/sh echo "Creating WAN Watchdog script at /usr/bin/wan-watchdog.sh..." cat << 'EOF' > /usr/bin/wan-watchdog.sh #!/bin/sh LED_BLUE="/sys/class/leds/blue:status/brightness" LED_GREEN="/sys/class/leds/green:status/brightness" LED_RED="/sys/class/leds/red:status/brightness" STATE_FILE="/tmp/wan_led_state" set_rgb() { local r=$1 g=$2 b=$3 local new_state="$r$g$b" # Only update if the state changed if [ "$(cat $STATE_FILE 2>/dev/null)" != "$new_state" ]; then echo "$r" > "$LED_RED" echo "$g" > "$LED_GREEN" echo "$b" > "$LED_BLUE" echo "$new_state" > "$STATE_FILE" fi } while true; do # Get the interface currently handling the default route ACTIVE_IF=$(ip route get 1.1.1.1 2>/dev/null | awk '/dev/ {print $5; exit}') if [ -z "$ACTIVE_IF" ]; then # No gateway found set_rgb 0 0 1 # Blue else # Try a quick ping through that specific interface if ping -I "$ACTIVE_IF" -c 1 -W 2 1.1.1.1 >/dev/null 2>&1; then set_rgb 1 1 1 # White else set_rgb 1 0 0 # Red fi fi sleep 10 done EOF echo "Creating Init Service at /etc/init.d/wan-led..." cat << 'EOF' > /etc/init.d/wan-led #!/bin/sh /etc/rc.common USE_PROCD=1 START=99 start_service() { procd_open_instance procd_set_param command /usr/bin/wan-watchdog.sh procd_set_param respawn procd_set_param stdout 1 procd_set_param stderr 1 procd_close_instance } stop_service() { # Reset LEDs when service stops echo 0 > /sys/class/leds/blue:status/brightness echo 0 > /sys/class/leds/green:status/brightness echo 1 > /sys/class/leds/red:status/brightness rm -f /tmp/wan_led_state } EOF echo "Setting executable permissions..." chmod +x /usr/bin/wan-watchdog.sh chmod +x /etc/init.d/wan-led echo "Adding files to /etc/sysupgrade.conf to survive backups..." grep -qxF '/usr/bin/wan-watchdog.sh' /etc/sysupgrade.conf || echo '/usr/bin/wan-watchdog.sh' >> /etc/sysupgrade.conf grep -qxF '/etc/init.d/wan-led' /etc/sysupgrade.conf || echo '/etc/init.d/wan-led' >> /etc/sysupgrade.conf echo "Securing startup sequence in /etc/rc.local..." if ! grep -q "/etc/init.d/wan-led" /etc/rc.local; then sed -i '/^exit 0/d' /etc/rc.local cat << 'EOF' >> /etc/rc.local # Ensure WAN LED watchdog is enabled and running after sysupgrade if [ -x /etc/init.d/wan-led ]; then /etc/init.d/wan-led enabled || /etc/init.d/wan-led enable /etc/init.d/wan-led start fi exit 0 EOF fi echo "Enabling and starting wan-led service..." /etc/init.d/wan-led enable /etc/init.d/wan-led start echo "Installation complete! Service is running and upgrade-proof."