Script that flashes Internet Red LED as a function of Internet reachability

I have developed a simple script that indicates the Internet status on my router by managing the Red Internet LED.
Tested working on my Buffalo WBMR-HP-G300H. Adapt it to your router if you don't have Red Internet LED.

Here you have, in case someone could consider it useful:

#!/usr/bin/env bash

# Adapta el LED Internet Rojo a parpadeo en función del la respuesta del comando ping hacia Internet:
# Ping a $DomaintoPing y a $IPtoPing correcto --> LED Rojo apagado.
# Ping a $DomaintoPing incorrecto y a $IPtoPing correcto --> LED Rojo parpadea 500ms de cada 1.000ms.
# Ping a $DomaintoPing y a $IPtoPing incorrectos --> LED Rojo fijo.
# LED Verde Internet no se ve alterado.

### Requisitos:
## Configuración base en "/etc/config/system" :
#config led
#    option name 'InternetOnline'
#    option sysfs 'wbmr:red:internet'
#    option trigger 'timer'
#    option default '0'
#    option delayon '0'
#    option delayoff '0'

version="v0.01Alfa - 2020-06-27"
IPtoPing="8.8.8.8"
DomaintoPing="www.google.com"
NPings=5
if ping -c $NPings $IPtoPing
then	# Internet vìa IP funcionando
	echo "[O] Internet via IP funcionando."
	if ping -c $NPings $DomaintoPing
	then
		#Internet en funcionamiento aparente:
		echo "[O] Internet via DNS funcionando."
#		echo 0 > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/delay_on
		echo none > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/trigger		# Apagamos LED rojo de Internet.
	# Medicion de calidad:
	PaquetesRecibidos="$( ping -c $NPings $DomaintoPing | tail -n 2 | head -n 1 | awk '{print $4}' )"
	if [ "$PaquetesRecibidos" -eq "$PaquetesRecibidos" ]
	then
		if [ $PaquetesRecibidos -lt $NPings ]
		then	# Algún ping perdido:
	    PorcionDeSegundo=$(( 1000 / NPings ))	# Número de milisegundos en una porción de segundo (para 5 porciones, PorcionDeSegundo=200 ).
	    DelayOff=$( expr $PaquetesRecibidos \* $PorcionDeSegundo )
	    DelayOn=100
			echo timer > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/trigger		# Ponemos LED rojo de Internet en modo Timer.
	    echo $DelayOff > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/delay_off
	    echo $DelayOn > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/delay_on
	  fi
	else
		echo "[X] Exit Code 1: valor no numérico leído para el número de pings recibidos."
		exit 1		# Exit Code 1: valor no numérico leído para el número de pings recibidos.
	fi
	else
		echo "[!] Internet via DNS funcionando."
	  # Internet en funcionamiento parcial:
		echo timer > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/trigger		# Ponemos LED rojo de Internet en modo Timer.
	  echo 500 > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/delay_off
	  echo 500 > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/delay_on
	fi
else	# Internet no funcionando.
	echo "[X] Internet no funcionando."
	echo default-on > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/trigger		# Encendemos (fijo) LED rojo de Internet.
#  echo 0 > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/delay_off
#  echo 500 > /sys/devices/platform/gpio-leds/leds/wbmr:red:internet/delay_on
fi

The script is internally (variables and comments) in spanish, sorry. Feel free to translate and publish it.

Basic behavior:

1.- Ping to domain name (www.google.com) and Internet IP (8.8.8.8) working --> Red Internet LED off.
1.a.- If any of the (default 5) pings fails (i.e: 1 out of 5) --> Short Red Internet LED blinking (100ms) and 200ms Red Internet LED off for each working ping (i.e: 800ms off for 4 out of 5 pings working).
2.- Ping to domain name (www.google.com) failing and Internet IP (8.8.8.8) working --> Red Internet LED blinking 500ms On/Off.
3.- Ping to domain name (www.google.com) and Internet IP (8.8.8.8) both failing --> Red Internet LED On (fixed).

About the 1.a case: the objective is to adapt Red Internet LED (short) blinking to the quality of the ping results: a faster blinking means more pings lost (i.e: 4 out of 5 pings lost blinks 100ms On and 200ms Off).

I call it via CRON:
* * * * * RedInternetLED-func-of-ping-results.sh

As can be seen in the comments, it is supposed to require (I am not sure) some config lines at "/etc/config/system" .

Notes:

  • Green Internet LED is not affected (could have its own behavior).
  • The default LED config at Buffalo WBMR-HP-G300H Wiki seems to put the Internet Green LED permanently On. I would rather suggest (I don't know if putting it on the Wiki; I will probably just link this thread):
config led 'led_internet'
        option name 'internet'
        option sysfs 'wbmr:green:internet'
        option trigger 'netdev'
        option mode 'tx rx'
       option dev 'pppoe-wan'
        option default '0'

... that blinks green only on data transfers. So, both green and red should armonically coexist.