OpenWrt custom service scripting error, advice needed

i have written a service to stay signed in to the BT Wi-fi network, is works as expected, but i have been trying to add a way to detect when the AP im connected to fails its connectivity check several times then it would scan for wi-fi and connect to the strongest "BTWi-fi" AP or the 2nd strongest signal if the current BSSID matches, i have added logger debug messages and the else part that increments the "CONNFAILCOUNT" variable is never triggered (tested with wi-fi hotspot on phone and mobile data off), any advice on what i may of missed would be awesome, thanks

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

##############-- BTWi-fi Openwrt Autologin Service --##############
##############- By: Aidan Macgregor (v.5 December 2022) -###############
# https://github.com/aidanmacgregor/
# (Tested On OpenWrt 19.07.10 & 21.02.3)

############### INFO ###############

#### Manual Run & Stop (Stopping Also Signs The Account Out)
# 	Click Start/Stop on the service in LUCI (System > Startup)

#### Automatically Start On Boot
#	Enable the service in LUCI (System > Startup)

#### In The Local Startup Section (System > Startup > Local Starup)
#Choose Your Account Type & Add Email & Password

#### Account Type:

# 	1 = BT Home Broadband
# 	2 = BT Buisness Broadband
# 	3 = BT Wi-Fi Account

###############- SETTINGS -###############

ACCOUNTTYPE=$(grep '#ACCTYPE=' /etc/rc.local | sed -r 's/^.{9}//')
USERNAME=$(grep '#USERNAME=' /etc/rc.local | sed -r 's/^.{10}//')
PASSWORD=$(grep '#PASSWORD=' /etc/rc.local | sed -r 's/^.{10}//')

###############- OPTIONAL -###############

LOGSIZE=$(grep '#LOGSIZE=' /etc/rc.local | sed -r 's/^.{9}//')
PING1=$(grep '#PING1=' /etc/rc.local | sed -r 's/^.{7}//')
PING2=$(grep '#PING2=' /etc/rc.local | sed -r 's/^.{7}//')
PING3=$(grep '#PING3=' /etc/rc.local | sed -r 's/^.{7}//')
LOGPATH=$(grep '#LOGPATH=' /etc/rc.local | sed -r 's/^.{9}//')
WIFISWITCH=$(grep '#LOGPATH=' /etc/rc.local | sed -r 's/^.{12}//')

##########################################
#####---- DO NOT EDIT BELOW HERE ----#####
##########################################

START=99
STOP=1
PIDFILELOGIN=/var/run/BT_Wi-fi_Autologin.pid
PIDFILECUTLOG=/var/run/BT_Wi-fi_Logfile.pid
PIDFILETLS=/var/run/TLS_Installer.pid
PIDFILEWIFISWITCH=/var/run/PIDFILEWIFISWITCH.pid
CONNFAILCOUNT=0

start() {
	logger -t BTWi-fi_Autologin_Service "$(date) BTWi-fi Autologin Service Started"
	echo "$(date) BTWi-fi Autologin Service Started" >> $LOGPATH
	[ -f $PIDFILELOGIN ] && [ ! -d /proc/`cat $PIDFILELOGIN` ] && rm $PIDFILELOGIN
	[ -f $PIDFILELOGIN ] && exit 1
	btwifi_loop &
	echo -n $! > $PIDFILELOGIN
	[ -f $PIDFILECUTLOG ] && [ ! -d /proc/`cat $PIDFILECUTLOG` ] && rm $PIDFILECUTLOG
	[ -f $PIDFILECUTLOG ] && exit 1
	cleanlog_loop &
	echo -n $! > $PIDFILECUTLOG
	[ -f $PIDFILETLS ] && [ ! -d /proc/`cat $PIDFILETLS` ] && rm $PIDFILETLS
	[ -f $PIDFILETLS ] && exit 1
	tls_loop &
	echo -n $! > $PIDFILETLS
	[ -f $PIDFILEWIFISWITCH ] && [ ! -d /proc/`cat $PIDFILEWIFISWITCH` ] && rm $PIDFILEWIFISWITCH
	[ -f $PIDFILEWIFISWITCH ] && exit 1
	wifiswitch_loop &
	echo -n $! > $PIDFILEWIFISWITCH
}

stop() {
	kill `cat $PIDFILELOGIN`
	kill `cat $PIDFILECUTLOG`
	kill `cat $PIDFILETLS`
	kill `cat $PIDFILEWIFISWITCH`
	rm $PIDFILELOGIN
	rm $PIDFILECUTLOG
	rm $PIDFILETLS
	rm $PIDFILEWIFISWITCH
	wget -T 2 -O /dev/null 'https://192.168.23.21:8443/accountLogoff/home?confirmed=true'
	logger -t BTWi-fi_Autologin_Service "$(date) BTWi-fi Autologin Service Stopped Manually (Or Reboot)"
	echo "$(date) BTWi-fi Autologin Service Stopped Manually (Or Reboot)" >> $LOGPATH
}

btwifi_loop(){
while true
do
	if ! ping -c 1 -W 1 $PING1 2>/dev/null >/dev/null	 
	then
	CONNFAILCOUNT=$((CONNFAILCOUNT + 1))
	logger -t BTWi-fi_Autologin_Service "$(date) FAIL CHECK"
	else
	CONNFAILCOUNT=0
	logger -t BTWi-fi_Autologin_Service "$(date) PASS CHECK"
	fi
		if ! ping -c 1 -W 1 $PING2 2>/dev/null >/dev/null
		then
		CONNFAILCOUNT=$((CONNFAILCOUNT + 1))
		logger -t BTWi-fi_Autologin_Service "$(date) FAIL CHECK"
		else
		CONNFAILCOUNT=0
		logger -t BTWi-fi_Autologin_Service "$(date) PASS CHECK"
		fi
			if ! ping -c 1 -W 1 $PING3 2>/dev/null >/dev/null
			then
			CONNFAILCOUNT=$((CONNFAILCOUNT + 1))
			logger -t BTWi-fi_Autologin_Service "$(date) FAIL CHECK"
			else
			CONNFAILCOUNT=0
			logger -t BTWi-fi_Autologin_Service "$(date) PASS CHECK"
			fi
				if [ "$ACCOUNTTYPE" = "1" ]
				then
				logger -t BTWi-fi_Autologin_Service "$(date) Offline, attempting login URL 1 (BT Home Broadband Account)"
				echo "$(date) Offline, attempting login URL 1 (BT Home Broadband Account)" >> $LOGPATH
				wget -T 2 -O /dev/null --post-data "username=$USERNAME&password=$PASSWORD" 'https://192.168.23.21:8443/tbbLogon'
				elif [ "$ACCOUNTTYPE" = "2" ]
				then
				logger -t BTWi-fi_Autologin_Service "$(date) Offline, attempting login URL 2 (BT Buisness Broadband Account)"
				echo "$(date) Offline, attempting login URL 2 (BT Buisness Broadband Account)" >> $LOGPATH
				wget -T 2 -O /dev/null --post-data "username=$USERNAME&password=$PASSWORD" 'https://192.168.23.21:8443/ante?partnerNetwork=btb'
				elif [ "$ACCOUNTTYPE" = "3" ]
				then
				logger -t BTWi-fi_Autologin_Service "$(date) Offline, attempting login URL 3 (BT Wi-Fi Account)"
				echo "$(date) Offline, attempting login URL 3 (BT Wi-Fi Account)" >> $LOGPATH
				wget -T 2 -O /dev/null --post-data "username=$USERNAME&password=$PASSWORD" 'https://192.168.23.21:8443/ante'
				fi

		   
		  
sleep 1
done
}

tls_loop(){
while true
do
if ! $(grep "DISTRIB_RELEASE='1" /etc/openwrt_release | sed -r 's/^.{17}//' | sed -r 's/(.{2}).*/\1/')
then
	echo "$(date) OpenWrt v19 Or Older Detected, TLS REQUIRED!" >> $LOGPATH
	logger -t TLS_Installer "$(date) OpenWrt v19 Or Older Detected, TLS REQUIRED!"
	if [ -e "/usr/lib/opkg/info/libustream-mbedtls20150806.list" ]
	then
		echo "$(date) TLS Is Installed! Exiting Install Loop" >> $LOGPATH
		logger -t TLS_Installer "$(date) TLS Is Installed! Exiting Install Loop"
		kill `cat $PIDFILETLS`
		rm $PIDFILETLS
		exit 0
	else
		echo "$(date) Checking Internet To Install TLS..." >> $LOGPATH
		logger -t TLS_Installer "$(date) Checking Internet To Install TLS..."
			if ping -c 2 -W 1 8.8.8.8 2>/dev/null >/dev/null
			then
				echo "$(date) Internet Connected Running Install" >> $LOGPATH
				logger -t TLS_Installer "$(date) Internet Connected Running Install"
				opkg update && opkg install libustream-mbedtls
			else
				echo "$(date) NO Internet Will Try Again In 30 Seconds" >> $LOGPATH
				logger -t TLS_Installer "$(date) NO Internet Will Try Again In 30 Seconds"
			fi
	fi
else
	echo "$(date) OpenWrt v20 Or Newer Detected, TLS NOT REQUIRED!" >> $LOGPATH
	logger -t TLS_Installer "$(date) OpenWrt v20 Or Newer Detected, TLS NOT REQUIRED!"
	kill `cat $PIDFILETLS`
	rm $PIDFILETLS
	exit 0
fi
sleep 30
done
}

cleanlog_loop(){
while true
do
if [[ "$LOGSIZE" -ge "$LOGSIZE" ]]
then
echo "$(tail -$LOGSIZE $LOGPATH)" > $LOGPATH
fi
sleep 60
done
}

wifiswitch_loop(){
while true
do
if [[ "$CONNFAILCOUNT" -gt "9" ]]
then
logger -t BTWi-fi_Autologin_Service "$(date) 9 failures detected"
	if [[ "$WIFISWITCH" = "1" ]]
	then
	CURRENT_BSSID=$(wifi status | grep "bssid" | awk '{print $2}')
	SCAN_RESULTS=$(iw dev wlan0 scan | grep "SSID: BTWi-fi")
	STRONGEST_NETWORK=$(echo "$SCAN_RESULTS" | sort -k6,6 -r | head -n 1)
	STRONGEST_BSSID=$(echo "$STRONGEST_NETWORK" | awk '{print $2}')
		if [[ "$CURRENT_BSSID" != "$STRONGEST_BSSID" ]]
		then
		wifi up ifname=wlan0 ssid=BTWi-fi bssid=$STRONGEST_BSSID
		CONNFAILCOUNT=0
		fi
	SECOND_STRONGEST_NETWORK=$(echo "$SCAN_RESULTS" | sort -k6,6 -r | head -n 2 | tail -n 1)
	SECOND_STRONGEST_BSSID=$(echo "$SECOND_STRONGEST_NETWORK" | awk '{print $2}')
		if [[ "$CURRENT_BSSID" != "$SECOND_STRONGEST_BSSID" ]]
		then
		wifi up ifname=wlan0 ssid=BTWi-fi bssid=$SECOND_STRONGEST_BSSID
		CONNFAILCOUNT=0
		fi
	fi
fi
sleep 30
done
}

I can only write common considerations.

  1. start consists of several loops, is it expected?
  2. Is CONNFAILCOUNT ever increased?

Maybe it will be better off as either a plugin for the @dibdot's travelmate package or being integrated into the travelmate package as a special case for BTWi-fi SSIDs.

I was thinking build it around travelmate as well, and also maybe look at watchcat for the connectivity testing. The OP's routines to parse the scan results are nowhere close to being workable.

Could a wifi connection fire off a hotplug script to log into BT?

Not sure about wifi hotplug scripts, maybe wwan interface hotplug? Either way, @dibdot is way more knowledgeable on this subject, having written travelmate. :wink: