[How To] Switch on a led when internet connection is available

In this example I used a green led to indicate that internet connection is available and a red one to warn when internet connection is not available (Router ADB P.DG A4001N1), but if you have just one led, you can set a fixed light when internet is available and a flash light when it is not available.

let's create the script:

nano /root/internet-check.sh

#start looping
while [ true ]; do 
#checking if internet is available (google)
/usr/bin/wget -o -q --spider http://google.com > /dev/null
if [[ $? -eq 0 ]]; then
#internet is available, switch on the green led and off the red one
/bin/echo "0" > /sys/devices/gpio-leds/leds/A4001N1:red:inet/brightness
/bin/echo "1" > /sys/devices/gpio-leds/leds/A4001N1:green:inet/brightness
#internet is not available, switch off the green led and on the red one
/bin/echo "0" > /sys/devices/gpio-leds/leds/A4001N1:green:inet/brightness
/bin/echo "1" > /sys/devices/gpio-leds/leds/A4001N1:red:inet/brightness
#sleep 60 seconds and then repeat checking
sleep 60

note: change ".../sys/devices/gpio-leds/leds/A4001N1:green:inet/..." with the correct path for your router.

then give the right permissions to the above script:

chmod 755 /root/internet-check.sh

test the script:

/bin/sh /root/internet-check.sh

to launch the script at the startup, put the following line right before the "exit 0" line in the "/etc/rc.local" file

/bin/sh /root/internet-check.sh &


Two minor points:

Obviously relies on nano, which is an "aftermarket" package.

Although perfectly valid, I believe a single ping to or with a short timeout would suffice and deliver the same result and not rely on the availability or reachability* of the web server. Also, for pppoe connections, this could be done in a hotplug script.

*) Is that a word? What I'm getting at is that google.com might be blocked upstream, or feel offended by getting a request every 30 seconds. DNS servers might be blocked too, but you're already relying on DNS resolution at this point anyway.


Good points!

Improved script:

while :

ping -c5 -q
case $status in
        echo "host is alive";
        echo "network unreachable or host not responding to pings";
        echo "No route to host or other error";
if [ $status -eq 0 ]; then
    /bin/echo "Internet is up"
    /bin/echo "1" > /sys/class/leds/tp-link:blue:wan/brightness
    /bin/echo "Internet is down"
    /bin/echo "0" > /sys/class/leds/tp-link:blue:wan/brightness
sleep 30


Based on your posts, and some googling, I have made my Archer C50 to show a orange led when no internet and a green one when yes.

There is a cron entry that excecutes a simple script with a ping based on this thread idea... which in turns excecutes 2 more small scripts (internet-on.sh and internet-off.sh).

Each one turns on a green (or orange) led and turns off the orange (or green) led if called. With de bash shebangs dont worked for me, so changed to #!/bin/sh. The main script does only one ping to google DNS.


ping -q -w 1 -c 1 && /usr/bin/internet-on.sh || /usr/bin/internet-off.sh


/bin/echo "1" > /sys/devices/platform/gpio-leds/leds/c50:green:wan/brightness
/bin/echo "0" > /sys/devices/platform/gpio-leds/leds/c50:orange:wan/brightness


/bin/echo "1" > /sys/devices/platform/gpio-leds/leds/c50:orange:wan/brightness
/bin/echo "0" > /sys/devices/platform/gpio-leds/leds/c50:green:wan/brightness

Last but not least. Here it is the content of my crontab, specifically the last line with the */3 which means every 3 minutes.

00 06 * * Mon,Tue,Wed,Thu,Fri,Sat,Sun /usr/bin/wifi_schedule.sh start
00 23 * * Mon,Tue,Wed,Thu,Fri,Sat,Sun /usr/bin/wifi_schedule.sh stop
*/3 * * * Mon,Tue,Wed,Thu,Fri,Sat,Sun /usr/bin/chki.sh

I moved the wlan LED funcionality to WPS (which I down use)

Why not just use a netdev trigger? https://openwrt.org/docs/guide-user/base-system/led_configuration

A word of caution: ICMP ping is not guaranteed to be delivered to the host and can be dropped by any host in-between. These get dropped easily in case of an overload.

You are right, you could -- and should -- use a netdev trigger or a hotplug script if you're using PPPoE/PPPoA. However, if your WAN connection is coming in through ethernet, your connection is purely physical, as long as your cable is plugged in it will always show "on" even if your upstream internet connection is down. I am assuming that is what @pilovis and @rodoviario are trying to accomplish here.


You are right, but if you have the ability to change your LED definition in the board configuration file (such as DTS) you could switch active high with active low, or vice versa.

Another why not -- why not use the hotplug script set to monitor WAN instead of crontab?

What strategy do you use to check connectivity?

Correct me if I wrong... but, I think that out-of-the-box, the luci-led configuration does detect well when you plug or unplug the WAN cable... and lights the corresponding LED in either case. I have moved that funcionality to the WPS LED, so it lits when there is a cable and a link to the upstream modem.
On the other hand, the tricks and scripts used aside the luci-LED gui allow me to ping a public IP and lit red if there is no answer, and green otherwise on the WAN LED.

Sidenote: With the 3 minute interval and only one ping to google DNS I avoid blocking, but also sometimes get false positives. It will show de red no-internet if there is a small glitch, for the next 3 minutes. I will fine-tune, but it is fine for me right now.

I used this tutorial and it's just excellent!
I am Using Multiple WAN using mwan3 1st WAN has unlimited plan and other has limited plan (for Backup) I was asking if there is a way to change LED to different colour if my router is using Backup WAN.

Like, MAIN WAN (Connected) EVEN (Backup is Disconnected) = Green
Backup WAN (Connected), Main WAN (disconnected) = Blue
Both WAN Disconnected = RED

It should be fairly easy using a small shell script, mwan3 does hotplug-like events. (I'm not using mwan3 so I can't give a more concrete example, sorry.)

1 Like