Keeping WAN Led blinking beside ON/OFF according to Internet Connectivity

After lots of fiddling I was able to create and fine-tune a code to do the following:
White LED when Internet is Connected
Amber LED when Internet is Disconnected
LED is off when WAN Ethernet cable is unplugged

These checks are are made every 10 seconds

#!/bin/sh /etc/rc.common

IP='8.8.8.8'
while true
do

	wan_status=$(swconfig dev switch0 port 4 get link | sed -r 's/.*link:([[:alnum:]]*).*/\1/')

	if [ "$wan_status" == "down" ] 
	then
		echo 0 > /sys/class/leds/pca963x:shelby:white:wan/brightness
		echo 0 > /sys/class/leds/pca963x:shelby:amber:wan/brightness
	else
		fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
		if [ "$?" = 0 ]
		then
  			echo "Host found"
			echo 255 > /sys/class/leds/pca963x:shelby:white:wan/brightness
			echo 0 > /sys/class/leds/pca963x:shelby:amber:wan/brightness
		else
  			echo "Host not found"
			echo 255 > /sys/class/leds/pca963x:shelby:amber:wan/brightness
			echo 0 > /sys/class/leds/pca963x:shelby:white:wan/brightness
		fi
	fi
	sleep 10
done
exit 0

However, I lost the basic functionally of the blinking led according to the TX/RX activity.

I thought about making the script an init.d script however, it will be only executed one and using the while loop yield the same results as above.
I also thought about using crontab but its granularity is 60 seconds. Way too much time.
Can someone shed any light to do this while keeping the above added functions operating? @jeff

Thanks

Rough outline:

If interface down, set trigger to none, turn on/off LEDs

If interface up and no ping, set trigger to none, turn on/off LEDs

If interface up and ping, set triggers to netdev and set netdev interface as desired.

I haven’t looked into how netdev triggers are set up, but you can poke it with LuCI and see what changes in the corresponding /sys/class/led entries.

If you want 10-s response time, a loop like that is a common way to do it.

Got it working..Here is the code for others to use. This is targeted for WRT1900ACS

#!/bin/sh /etc/rc.common

IP='8.8.8.8'
while true
do

	wan_status=$(swconfig dev switch0 port 4 get link | sed -r 's/.*link:([[:alnum:]]*).*/\1/')
	
	#Check if interface is down, Turn off LEDs
	if [ "$wan_status" == "down" ] 
	then
		echo 0 > /sys/class/leds/pca963x:shelby:white:wan/brightness
		echo 0 > /sys/class/leds/pca963x:shelby:amber:wan/brightness

	#Check if interface is Up	
	elif [ "$wan_status" == "up" ]
	then
	
		fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
		SUCCESS=$?

		if [ $SUCCESS -eq 0 ]
		then
			echo "$IP replied"
			echo 255 > /sys/class/leds/pca963x:shelby:white:wan/brightness
			echo 0 > /sys/class/leds/pca963x:shelby:amber:wan/brightness
			echo netdev > /sys/class/leds/pca963x:shelby:white:wan/trigger
			echo eth0 > /sys/class/leds/pca963x:shelby:white:wan/device_name
			echo 'link tx rx' > /sys/class/leds/pca963x:shelby:white:wan/mode
		else
  			echo "$IP has not replied"
			echo 255 > /sys/class/leds/pca963x:shelby:amber:wan/brightness
			echo 0 > /sys/class/leds/pca963x:shelby:white:wan/brightness
			echo none > /sys/class/leds/pca963x:shelby:white:wan/trigger
		fi
	fi
	sleep 10
done
exit 0
2 Likes

Brevity is the sister of talent:

Can be:

		if fping -c1 -t300 $IP 2>/dev/null 1>/dev/null; then

Also, fping?

I am not experienced with Bash Scripting, so I will take your edit into consideration to clean the script.

Yes. I am using fping tool instead of the default ping. It is much faster If I need more pings, I can ping subnet hosts in a more convenient way rather than determining the Host ip. Also, I can test if particular TCP ports per host are up and can also check if a host alive/up instead of checking for "0" response from an icmp request, but I haven't used it

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.