Check connectivity and switch on/off a LED (GL-AR150)
Written by pmd - - no commentsShell script that will check if there is connectivity to a defined website every 60 seconds and switch ON/OFF the led :
- /usr/bin/WANLED :
#!/bin/sh
while [ true ]; do
/usr/bin/wget -q --tries=2 --spider https://www.google.com
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
else
#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
fi
sleep 60
done
while [ true ]; do
/usr/bin/wget -q --tries=2 --spider https://www.google.com
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
else
#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
fi
sleep 60
done
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
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
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
START=99
STOP=1
start(){
/usr/bin/WANLED &
}
stop(){
killall -9 WANLED
}
START=99
STOP=1
start(){
/usr/bin/WANLED &
}
stop(){
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
# 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
--------------------|--------------------|---------|--------------
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