Skip to content

Commit

Permalink
ffac-ssid-changer: fix shellcheck issues
Browse files Browse the repository at this point in the history
Fixed all issues except SC3057.
  • Loading branch information
grische authored and maurerle committed Nov 20, 2023
1 parent c7695ab commit 1af59ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion ffac-ssid-changer/gluonShellDiet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

# This script requires a file as argument in which it will remove all comment lines that start with a hash '#'

sed '/^\s*\#[^!].*/d; /^\s*\#$/d' $1
sed '/^\s*\#[^!].*/d; /^\s*\#$/d' "$1"
78 changes: 40 additions & 38 deletions ffac-ssid-changer/shsrc/ssid-changer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@ SETTINGS_DEBUG_LOGGING="$(uci -q get ssid-changer.settings.debug_log_enabled)"

pgrep -f autoupdater >/dev/null && safety_exit 'autoupdater running'
UT=$(sed 's/\..*//g' /proc/uptime)
[ $UT -gt 60 ] || safety_exit 'less than one minute'
[ "$UT" -gt 60 ] || safety_exit 'less than one minute'
[ "$(find /var/run -name "hostapd-phy*" | wc -l)" -gt 0 ] || safety_exit 'no hostapd-phy*'

# only once every timeframe minutes the SSID will change to the Offline-SSID
# (set to 1 minute to change immediately every time the router gets offline)
MINUTES="$(uci -q get ssid-changer.settings.switch_timeframe)"
: ${MINUTES:=30}
: "${MINUTES:=30}"

# the first few minutes directly after reboot within which an Offline-SSID always may be activated
# (must be <= switch_timeframe)
FIRST="$(uci -q get ssid-changer.settings.first)"
: ${FIRST:=5}
: "${FIRST:=5}"

# the Offline-SSID will start with this prefix use something short to leave space for the nodename
# (no '~' allowed!)

PREFIX="$(uci -q get ssid-changer.settings.prefix)"
: ${PREFIX:='FF_Offline_'}
: "${PREFIX:=FF_Offline_}"

PREFIX_OWE="$(uci -q get ssid-changer.settings.prefix_owe)"
: ${PREFIX_OWE:='FF_Off_OWE'}
: "${PREFIX_OWE:=FF_Off_OWE}"

if [ "$(uci -q get ssid-changer.settings.enabled)" = '0' ]; then
DISABLED='1'
Expand All @@ -47,20 +48,20 @@ fi

# generate the ssid with either 'nodename', 'mac' or to use only the prefix set to 'none'
SETTINGS_SUFFIX="$(uci -q get ssid-changer.settings.suffix)"
: ${SETTINGS_SUFFIX:='nodename'}
: "${SETTINGS_SUFFIX:=nodename}"

if [ $SETTINGS_SUFFIX = 'nodename' ]; then
if [ "$SETTINGS_SUFFIX" = 'nodename' ]; then
SUFFIX="$(uname -n)"
# 32 would be possible as well
if [ ${#SUFFIX} -gt $((30 - ${#PREFIX})) ]; then
# calculate the length of the first part of the node identifier in the offline-ssid
HALF=$(( (28 - ${#PREFIX} ) / 2 ))
# jump to this charakter for the last part of the name
SKIP=$(( ${#SUFFIX} - $HALF ))
SKIP=$(( ${#SUFFIX} - HALF ))
# use the first and last part of the nodename for nodes with long name
SUFFIX=${SUFFIX:0:$HALF}...${SUFFIX:$SKIP:${#SUFFIX}}
fi
elif [ $SETTINGS_SUFFIX = 'mac' ]; then
elif [ "$SETTINGS_SUFFIX" = 'mac' ]; then
SUFFIX="$(uci -q get network.bat0.macaddr | /bin/sed 's/://g')"
else
# 'none'
Expand All @@ -76,7 +77,7 @@ ONLINE_SSID_OWE="$(uci -q get wireless.owe_radio0.ssid)"
# get all SSIDs (replace \' with TICX and back to keep a possible tic in an SSID)
ONLINE_SSIDs="$(uci show | grep "wireless.client_radio[0-9]\." | grep ssid | awk -F '=' '{print $2}' | sed "s/\\\'/TICX/g" | tr \' \~ | sed "s/TICX/\\\'/g" ) "
# if for whatever reason ONLINE_SSIDs is NULL:
: ${ONLINE_SSIDs:="~FREIFUNK~"}
: "${ONLINE_SSIDs:=~FREIFUNK~}"

# temp file to count the offline incidents during switch_timeframe
TMP=/tmp/ssid-changer-count
Expand All @@ -87,15 +88,15 @@ TQ_LIMIT_ENABLED="$(uci -q get ssid-changer.settings.tq_limit_enabled)"
# if true, the offline ssid will only be set if there is no gateway reacheable
# upper and lower limit to turn the offline_ssid on and off
# in-between these two values the SSID will never be changed to preven it from toggeling every Minute.
: ${TQ_LIMIT_ENABLED:='0'}
: "${TQ_LIMIT_ENABLED:=0}"

if [ $TQ_LIMIT_ENABLED = 1 ]; then
if [ "$TQ_LIMIT_ENABLED" = 1 ]; then
TQ_LIMIT_MAX="$(uci -q get ssid-changer.settings.tq_limit_max)"
# upper limit, above that the online SSID will be used
: ${TQ_LIMIT_MAX:='45'}
: "${TQ_LIMIT_MAX:=45}"
TQ_LIMIT_MIN="$(uci -q get ssid-changer.settings.tq_limit_min)"
# lower limit, below that the offline SSID will be used
: ${TQ_LIMIT_MIN:='35'}
: "${TQ_LIMIT_MIN:=35}"
# grep the connection quality of the currently used gateway
GATEWAY_TQ=0
if [ -f /var/gluon/state/has_default_gw4 ]; then
Expand All @@ -104,9 +105,9 @@ if [ $TQ_LIMIT_ENABLED = 1 ]; then
MSG="TQ is $GATEWAY_TQ, "
if [ $GATEWAY_TQ -ge $TQ_LIMIT_MAX ]; then
if [ "$GATEWAY_TQ" -ge "$TQ_LIMIT_MAX" ]; then
CHECK=1
elif [ $GATEWAY_TQ -lt $TQ_LIMIT_MIN ]; then
elif [ "$GATEWAY_TQ" -lt "$TQ_LIMIT_MIN" ]; then
CHECK=0
else
# this is just get a clean run if we are in-between the grace periode
Expand All @@ -122,8 +123,8 @@ else
fi
fi
UP=$(($UT / 60))
M=$(($UP % $MINUTES))
UP=$((UT / 60))
M=$((UP % MINUTES))
HUP_NEEDED=0
if [ "$CHECK" -gt 0 ] || [ "$DISABLED" = '1' ]; then
Expand All @@ -132,29 +133,29 @@ if [ "$CHECK" -gt 0 ] || [ "$DISABLED" = '1' ]; then
# check status for all physical devices
for HOSTAPD in /var/run/hostapd-phy*; do
[ -e "$HOSTAPD" ] || break # handle the case of no hostapd-phy* files
ONLINE_SSID="$(echo $ONLINE_SSIDs | awk -F '~' -v l=$((LOOP*2)) '{print $l}')"
ONLINE_SSID="$(echo "$ONLINE_SSIDs" | awk -F '~' -v l=$((LOOP*2)) '{print $l}')"
LOOP=$((LOOP+1))
CURRENT_SSID="$(grep "^ssid=$ONLINE_SSID" $HOSTAPD | cut -d"=" -f2)"
CURRENT_SSID="$(grep "^ssid=$ONLINE_SSID" "$HOSTAPD" | cut -d"=" -f2)"
if [ "$CURRENT_SSID" = "$ONLINE_SSID" ]; then
log_debug "SSID $CURRENT_SSID is correct, nothing to do"
break
fi
CURRENT_SSID="$(grep "^ssid=$OFFLINE_SSID" $HOSTAPD | cut -d"=" -f2)"
CURRENT_SSID="$(grep "^ssid=$OFFLINE_SSID" "$HOSTAPD" | cut -d"=" -f2)"
if [ "$CURRENT_SSID" = "$OFFLINE_SSID" ]; then
# set online
logger -s -t "ffac-ssid-changer" -p 5 $MSG"SSID is $CURRENT_SSID, change to $ONLINE_SSID"
sed -i "s~^ssid=$CURRENT_SSID~ssid=$ONLINE_SSID~" $HOSTAPD
logger -s -t "ffac-ssid-changer" -p 5 "$MSG""SSID is $CURRENT_SSID, change to $ONLINE_SSID"
sed -i "s~^ssid=$CURRENT_SSID~ssid=$ONLINE_SSID~" "$HOSTAPD"
# HUP here would be to early for dualband devices
HUP_NEEDED=1
else
logger -s -t "ffac-ssid-changer" -p 5 "could not set to online state: did neither find SSID '$ONLINE_SSID' nor '$OFFLINE_SSID'. Please reboot"
fi
if [ "$OWE" = true ]; then
CURRENT_SSID_OWE="$(grep "^ssid=$OFFLINE_SSID_OWE" $HOSTAPD | cut -d"=" -f2)"
CURRENT_SSID_OWE="$(grep "^ssid=$OFFLINE_SSID_OWE" "$HOSTAPD" | cut -d"=" -f2)"
if [ "$CURRENT_SSID_OWE" = "$OFFLINE_SSID_OWE" ]; then
# set online
logger -s -t "ffac-ssid-changer" -p 5 $MSG"OWE SSID is $CURRENT_SSID_OWE, change to $ONLINE_SSID_OWE"
sed -i "s~^ssid=$CURRENT_SSID_OWE~ssid=$ONLINE_SSID_OWE~" $HOSTAPD
logger -s -t "ffac-ssid-changer" -p 5 "$MSG""OWE SSID is $CURRENT_SSID_OWE, change to $ONLINE_SSID_OWE"
sed -i "s~^ssid=$CURRENT_SSID_OWE~ssid=$ONLINE_SSID_OWE~" "$HOSTAPD"
# HUP here would be to early for dualband devices
HUP_NEEDED=1
else
Expand All @@ -164,41 +165,41 @@ if [ "$CHECK" -gt 0 ] || [ "$DISABLED" = '1' ]; then
done
elif [ "$CHECK" -eq 0 ]; then
log_debug "node is considered offline"
if [ $UP -lt $FIRST ] || [ $M -eq 0 ]; then
if [ $UP -lt "$FIRST" ] || [ $M -eq 0 ]; then
# set SSID offline, only if uptime is less than FIRST or exactly a multiplicative of switch_timeframe
if [ $UP -lt $FIRST ]; then
if [ $UP -lt "$FIRST" ]; then
T=$FIRST
else
T=$MINUTES
fi
#echo minute $M, check if $OFF_COUNT is more than half of $T
if [ $OFF_COUNT -ge $(($T / 2)) ]; then
if [ "$OFF_COUNT" -ge $((T / 2)) ]; then
# node was offline more times than half of switch_timeframe (or than $FIRST)
LOOP=1
for HOSTAPD in /var/run/hostapd-phy*; do
[ -e "$HOSTAPD" ] || break # handle the case of no hostapd-phy* files
ONLINE_SSID="$(echo $ONLINE_SSIDs | awk -F '~' -v l=$((LOOP*2)) '{print $l}')"
ONLINE_SSID="$(echo "$ONLINE_SSIDs" | awk -F '~' -v l=$((LOOP*2)) '{print $l}')"
LOOP=$((LOOP+1))
CURRENT_SSID="$(grep "^ssid=$OFFLINE_SSID" $HOSTAPD | cut -d"=" -f2)"
CURRENT_SSID="$(grep "^ssid=$OFFLINE_SSID" "$HOSTAPD" | cut -d"=" -f2)"
if [ "$CURRENT_SSID" = "$OFFLINE_SSID" ]; then
log_debug "SSID $CURRENT_SSID is correct, nothing to do"
break
fi
CURRENT_SSID="$(grep "^ssid=$ONLINE_SSID" $HOSTAPD | cut -d"=" -f2)"
CURRENT_SSID="$(grep "^ssid=$ONLINE_SSID" "$HOSTAPD" | cut -d"=" -f2)"
if [ "$CURRENT_SSID" = "$ONLINE_SSID" ]; then
# set offline
logger -s -t "ffac-ssid-changer" -p 5 $MSG"$OFF_COUNT times offline, SSID is $CURRENT_SSID, change to $OFFLINE_SSID"
sed -i "s~^ssid=$ONLINE_SSID~ssid=$OFFLINE_SSID~" $HOSTAPD
logger -s -t "ffac-ssid-changer" -p 5 "$MSG""$OFF_COUNT times offline, SSID is $CURRENT_SSID, change to $OFFLINE_SSID"
sed -i "s~^ssid=$ONLINE_SSID~ssid=$OFFLINE_SSID~" "$HOSTAPD"
HUP_NEEDED=1
else
logger -s -t "ffac-ssid-changer" -p 5 "could not set to offline state: did neither find SSID '$ONLINE_SSID' nor '$OFFLINE_SSID'. Please reboot"
fi
if [ "$OWE" = true ]; then
CURRENT_SSID_OWE="$(grep "^ssid=$OFFLINE_SSID_OWE" $HOSTAPD | cut -d"=" -f2)"
CURRENT_SSID_OWE="$(grep "^ssid=$OFFLINE_SSID_OWE" "$HOSTAPD" | cut -d"=" -f2)"
if [ "$CURRENT_SSID_OWE" = "$ONLINE_SSID_OWE" ]; then
# set offline
logger -s -t "ffac-ssid-changer" -p 5 $MSG"$OFF_COUNT times offline, SSID is $CURRENT_SSID_OWE, change to $OFFLINE_SSID_OWE"
sed -i "s~^ssid=$ONLINE_SSID_OWE~ssid=$OFFLINE_SSID_OWE~" $HOSTAPD
logger -s -t "ffac-ssid-changer" -p 5 "$MSG""$OFF_COUNT times offline, SSID is $CURRENT_SSID_OWE, change to $OFFLINE_SSID_OWE"
sed -i "s~^ssid=$ONLINE_SSID_OWE~ssid=$OFFLINE_SSID_OWE~" "$HOSTAPD"
HUP_NEEDED=1
else
logger -s -t "ffac-ssid-changer" -p 5 "could not set to offline state: did neither find SSID '$ONLINE_SSID_OWE' nor '$OFFLINE_SSID_OWE'. Please reboot"
Expand All @@ -208,7 +209,7 @@ elif [ "$CHECK" -eq 0 ]; then
fi
#else echo minute $M, just count $OFF_COUNT
fi
echo "$(($OFF_COUNT + 1))">$TMP
echo "$((OFF_COUNT + 1))">$TMP
fi
if [ $HUP_NEEDED = 1 ]; then
Expand All @@ -217,6 +218,7 @@ if [ $HUP_NEEDED = 1 ]; then
## check for nonmachting hotapd-pidfiles
if [ -f /lib/gluon/eulenfunk-hotfix/check_hostapd.sh ] ; then
sleep 2 # settle down
# shellcheck disable=SC2009
ps|grep hostapd|grep .pid|xargs -n 10 /lib/gluon/eulenfunk-hotfix/check_hostapd.sh
fi
HUP_NEEDED=0
Expand Down

0 comments on commit 1af59ce

Please sign in to comment.