Internet & Gateway LED indicator

Hi folks, looking for a little assistance with some scripting. The goal is to make an LED indicator on my APs that behaves as follows:

  1. If the internet is available: LED ON
  2. If the internet is not available but the default gateway is reachable: LED BLINKS
  3. If the internet is not available and the default gateway is not reachable: LED OFF

I have the internet test working with help from another post:

#!/bin/sh
#
#checking if internet is available (google)
/usr/bin/wget -o -q --spider http://google.com > /dev/null
if [[ $? -eq 0 ]]; then
#internet is available
/bin/echo "default_on" > /sys/devices/gpio-leds/leds/blue:iot/trigger
else
#internet is not available
/bin/echo "none" > /sys/devices/gpio-leds/leds/blue:iot/trigger
fi

How would I similarly script the ping test for the default gateway? Assume the gateway can change as these devices are configured via DHCP.

Thank you!

Why not simply ping IP addresses instead of host names?
Guess you can ping hostname for all but using hostnames usually only means extra chance for failure if the resolver haven’t wakened after a reboot.
If it is your gateway, simply stop giving it a random IP by using static lease for the important network devices.

Perhaps I was not specific, I was planing to ping the IP, but need to be able to programatically get the IP as the device could be on any network with any gateway. This script does the trick:

gateway=$(route -n | grep 'UG[ \t]' | awk '{print $2}')
ping -c 1 $gateway &> /dev/null
if [ "$?" = 0 ]; then
#gateway is available
/bin/echo "timer" > /sys/class/leds/blue:iot/trigger
else
#gateway is not available
/bin/echo "none" > /sys/class/leds/blue:iot/trigger
fi

Now the next question, if I want this to run every 30 seconds, do I just put it in a loop with a sleep? Or does OpenwWRT have a better solution for this?

Google have fixed 1.1.1.1 or 8.8.8.8 as IP address.

Since cron has a min interval of 1min you will need to make your own sleep function in the script to get a 30sec loop.

Yes, the intent was to have a 2-tier test, first check the ping to the local gateway to see if the local network infrastructure is working and if so flash the light. Then check DNS and Internet is working and if so put the light on solid.

This way I can give people some very basic debugging instructions. If the light is off, reboot the APs/router, if the light is flashing, you need to call the ISP.

Thanks.

See this for reference on a script that takes time spent pinging into account: Leds on based on ping through specific interface, possible? - #11 by Cthulhu88

You also need to set brightness to 0 after setting trigger to none. If brightness was left at 1 (or 255) by the timer (or previously default-on), simply setting the trigger to "none" won't turn it off.

You can also improve the way you fetch the gateway address with this:

route -n | awk '($4 == "UG") {print $2}'
1 Like