Change GL-MT1300 led status on vpn connection

I'm using a gl.inet mt1300 router as travelrouter. It has is own fork of OpenWrt so please bear with me a bit..
( OpenWrt 19.07.7, r11306-c4a6851c72)

(I didn't go to Openwrt snapshot because of the wifidrivers not working properly)

The router has a blue and a white led, this (I think) script below makes the led blink blue when there is no internet connection en solid white when there is. As far as I can see, it does this by using ping.

I want to alter the script so it doesn't only check if it can ping, but when it can ping AND it goes through tun0 then the led must be set to white_breath so I can see visualy it's connected to vpn.

So if it pings when tun0 is down, it does what the script does at this moment and when tun0 is up it changes the led status, and back when tun0 goes down again.

When I ping with vpn on:

root@GL-MT1300:/usr/bin# ping -I tun0 1.1.1.1
PING 1.1.1.1 (1.1.1.1): 56 data bytes
64 bytes from 1.1.1.1: seq=0 ttl=59 time=19.911 ms
64 bytes from 1.1.1.1: seq=1 ttl=59 time=44.984 ms

When I ping with vpn off:

`root@GL-MT1300:/usr/bin# ping -I tun0 1.1.1.1
ping: bad address 'tun0'

The script that I want to edit;

#!/bin/sh

status="0"
count=`uci get mwan3.wan.count 2>/dev/null`
timeout=`uci get mwan3.wan.timeout 2>/dev/null`
track_ip=`uci get mwan3.wan.track_ip 2>/dev/null`
[ -z "$count" ] && count="1"
[ -z "$timeout" ] && timeout="2"
[ -z "$track_ip" ] && track_ip="1.1.1.1 1.0.0.1"

mt1300_led off
mt1300_led blue_breath daemon

for ip in $track_ip;
do
	ping -c $count -W $timeout -q $ip 1>/dev/null
	ret=`echo $?`
	if [ "$ret" = "0" ];then
		status="1"
		break
	fi
done

while true
do
	if [ "$status" = "0" ];then
		mt1300_led blue_breath daemon
	else
		mt1300_led white daemon
	fi

	sleep 5

	#mwan3 will not be used in the AP/WDS mode
	mode=`uci -q get glconfig.bridge.mode`
	if [ "$mode" = "ap" -o "$mode" = "wds" ];then
		[ "$status" = 1 ] && sleep 10
		for ip in $track_ip;
		do
			ping -c $count -W $timeout -q $ip 1>/dev/null
			ret=`echo $?`
			if [ "$ret" = "0" ];then
				status="1"
				break
			else
				status="0"
			fi
		done
		continue
	fi
	

	if [ "$status" = "0" ];then
		interface=`get_wan_interface`
		if [ -z "$interface" ];then
			continue
		fi

		for ip in $track_ip;
		do
			ping -c $count -W $timeout -q $ip 1>/dev/null
			ret=`echo $?`
			if [ "$ret" = "0" ];then
				status="1"
				break
			fi
		done
	else
		interface=`get_wan_interface`
		if [ -z "$interface" ];then
			status="0"
			continue
		fi
		
		mwan3_status=`mwan3 policies | grep "Current ipv4 policies:" -A 2 | tail -n 1 | sed 's/ //g' 2>/dev/null`
		if [ "$mwan3_status" = "default" ];then
			for ip in $track_ip;
			do
				ping -c $count -W $timeout -q $ip 1>/dev/null
				ret=`echo $?`
				if [ "$ret" = "0" ];then
					status="1"
					break
				else
					status="0"
				fi
			done
			continue
		fi

		mwan3_interface=`echo $mwan3_status | awk -F \( '{print $1}' 2>/dev/null`
		check=`echo $interface | grep $mwan3_interface 2>/dev/null`
		if [ -z "$check" ];then
			for ip in $track_ip;
			do
				ping -c $count -W $timeout -q $ip 1>/dev/null
				ret=`echo $?`
				if [ "$ret" = "0" ];then
					status="1"
					break
				else
					status="0"
				fi
			done
		fi	
	fi
done

something like this

Oops I was so occupied with the issue I didn't search on this forum .. my bad. I check the posts.

I can't get my head around it, maybe it's the good weather overhere :wink:

I've got three options.

  1. ping works over tun0 (so including the -I tun0 option)
  2. ping works over normal internet but tun0 is down
  3. ping isn't working

Options led on MT1300

Usage: ./mt1300_led <option>
Option:
      off
      blue
      white
      both
      blue_flash <normal|medium|fast>
      white_flash <normal|medium|fast>
      both_flash <normal|medium|fast>
      blue_breath
      white_breath
      both_breath

So I removed the script I just posted, to make sure all is working fine without the script (all leds are down)

With if else fi it is easy, but I've got a third option, how to I accomplish this in a script?

I'm a script noob, that obvious :wink:

something with elif maybe?

So i think the following;

#!/bin/sh

# set default led
mt1300_led off
mt1300_led blue_breath daemon

So the options ping works with tun0 and without tun0 need to be addressed, if one of the two is not true, then the default of the loop sets in. (mt1300_led blue_breath daemon)

Try this. It should work that way:

  1. Ping works and interface tunX exists – blink white
  2. Ping works and interface tunX doesn’t exist – solid white
  3. Ping doesn’t work – blink blue
#!/bin/sh

ip="8.8.8.8"
count="1"
timeout="1"
mt1300_led off
mt1300_led blue_breath daemon

while true; do

status=$(ping -q -c "$count" -W "$timeout" "$ip"  > /dev/null 2>&1 && echo "ok" || echo "fail")

if [ "$status" = "ok" ]; then
	if [ -d /sys/class/net/tun* ]; then    
		mt1300_led white_breath daemon   
	else    
		mt1300_led white daemon      
	fi
else
	mt1300_led blue_breath daemon
fi

sleep 10

done

1 Like

How simple can it be :wink:

Thanks a lot you all, @frollic for make me think on my own and @pavelgl for the copy and paste solution :wink:

We can discuss later with a beer in our hand at a bonfire what is the better help :wink:

Thanks guys.

It's been a while since I've used OpenVPN, but as far as I remember, you could assign a status/RX?TX of an OpenVPN connection to a LED without any scripts. Because wireguard is netifd compliant it's a bit easier with the wireguard, but there was a way to make it work for an OpenVPN too.