Leds on based on ping through specific interface, possible?

Right, -z doesn't work for -z 0, only if empty or unset.

Your crontabs can overlap, if the sum of the blocking time (via timeout) exceeds 6 seconds. If you want to execute it more than once every minute, use a loop, /proc/uptime and sleep.

INTERFACE_WAN="this must be the wan interface as shown in network.wan.device"
INTERFACE_WG="this must be the wireguard interface as shown in network.surfshark.device"
TARGET="target address"
TIMEOUT=5
DELAY=6

while true; do
    start=$(awk '{ sub(/\..*/, "") } { print }' /proc/uptime)

    if ping -q -4 -I $INTERFACE_WAN -c 1 -W $TIMEOUT $TARGET > /dev/null 2>&1; then
        if ping -q -4 -I $INTERFACE_WG -c 1 -W $TIMEOUT $TARGET > /dev/null 2>&1; then
            echo 0 > /sys/class/leds/red:system/brightness
            echo 255 > /sys/class/leds/blue:system/brightness
        else
            echo 0 > /sys/class/leds/blue:system/brightness
            echo 255 > /sys/class/leds/red:system/brightness
        fi
    else
        echo 0 > /sys/class/leds/red:system/brightness
        echo 0 > /sys/class/leds/blue:system/brightness
    fi

    end=$(awk '{ sub(/\..*/, "") } { print }' /proc/uptime)

    delay=$((DELAY - (end - start)))

    [ $delay -gt 0 ] && sleep $delay
done

Then add it to /etc/rc.local instead of crontab, sh script.sh & or ./script.sh &.

Other than using a loop and sleep, these are the things that I've changed:

  • Don't bother pinging the wireguard interface if the WAN ping failed.
  • Turn off the previous LED before turning on the next one.

Also, I've noticed you're turning both LEDs on when only the WAN interface is successful. I am assuming this is an error on your part, but if my assumption is wrong, just change the value for the new code.

The watchcat package ( https://openwrt.org/docs/guide-user/advanced/watchcat ) combines a similar shell script with a service.
The guide page doesn't list it, but there is a run_script mode: https://github.com/openwrt/packages/blob/master/utils/watchcat/files/watchcat.sh#L262-L265
The problem is, the script is only called after ping has failed for X amount of seconds, making it harder to customize your LEDs through it.

1 Like