Check connectivity and switch on/off a LED (GL-AR150)

Written by pmd - - no comments

Shell script that will check if there is connectivity to a defined website every 60 seconds and switch ON/OFF the led :

  • /usr/bin/WANLED :
while [ true ]; do
        /usr/bin/wget -q --tries=2 --spider
        if [ $? -eq 0 ]; then
                #echo "Connected ! LED RED OFF. LED GREEN ON."
                echo "none" >  /sys/class/leds/orange:wlan/trigger
                echo "default-on" >  /sys/class/leds/green:configurable/trigger
                #echo "Not connected ! LED RED ON. LED GREEN OFF."
                echo "default-on" >  /sys/class/leds/orange:wlan/trigger
                echo "none" >  /sys/class/leds/green:configurable/trigger
        sleep 60

Check which LEDs are available and modify in above script if necessary:

root@OpenWrt:~# ls /sys/class/leds
ath9k-phy0          green:configurable  green:power         orange:wlan

If --tries option is not recognized, you may need to install proper wget. Check like this:

root@OpenWrt:~# ls -la $(which wget)
lrwxrwxrwx    1 root     root            18 Apr 27 20:28 /usr/bin/wget -> /bin/uclient-fetch # Need to get proper wget
root@OpenWrt:~# opkg install wget-ssl
Downloading [...]
Signature check passed.
root@OpenWrt:~# ls -la $(which wget)
lrwxrwxrwx    1 root     root            21 May 21 06:16 /usr/bin/wget -> /usr/libexec/wget-ssl # No need to get proper wget

Schell script to autostart the above script :

  • /etc/init.d/WANLED :
#!/bin/sh /etc/rc.common


        /usr/bin/WANLED &

        killall -9 WANLED

Now let's make these script executable and started at startup:

# chmod +x /usr/bin/WANLED
# chmod +x /etc/init.d/WANLED
# /etc/init.d/WANLED enable
# /etc/init.d/WANLED start

Now the orange LED should be ON when there is no connectivity to Google.

LEDs may be driven by other component. To be sure it is not, go to System > LED Configuration.
In my case it looks like this:

 Name               | LED Name           | Trigger |
 green:power        | green:power        | none    | ☰EditDelete
 green:configurable | green:configurable | none    | ☰EditDelete
 orange:wlan        | orange:wlan        | none    | ☰EditDelete

FYI OpenWRT in use was : OpenWrt 22.03.5, r20134-5f15225c1e

Source: LED, Start script at startup, LED on when Internet is available

Comments are closed.