Building a script to switch between SSIDs, troubleshooting

Hello,

New here to the forum so let me know if I need to move this somewhere. Been reading posts and openwrt documentation for a few hours now and mostly got my script working other than one small problem. Every time I toggle the switch the $action always reads “ON”.

Here is my script I’m adding to the /usr/bin/switchaction file;

#!/bin/sh                                                                                                                                                                                                   
. /lib/functions/gl_util.sh                                                                                                                                                                                 
                                                                                                                                                                                                            
toggle_tor(){                                                                                                                                                                                               
        local action=$1                                                                                                                                                                                     
        if [ "$action" = "OFF" ];then                                                                                                                                                                       
                tor_switch off                                                                                                                                                                              
        else                                                                                                                                                                                                
                tor_switch on                                                                                                                                                                               
        fi                                                                                                                                                                                                  
}                                                                                                                                                                                                           
                                                                                                                                                                                                            
toggle_wifi(){                                                                                                                                                                                              
        local action=$1                                                                                                                                                                                     
        if [ "$action" = "OFF" ]; then                                                                                                                                                                      
                uci set wireless.default_radio0.ssid='SSID1'                                                                                                                                               
                uci set wireless.default_radio0.hidden='1'                                                                                                                                                  
                uci set wireless.default_radio1.ssid='SSID1'                                                                                                                                               
                uci set wireless.default_radio1.hidden='1'                                                                                                                                                  
                uci commit wireless                                                                                                                                                                         
                wifi reload                                                                                                                                                                                 
                logger -p notice -t wifi-toggle "SSID1"                                                                                                                                                    
                                                                                                                                                                            
        fi                                                                                                                                                                                                  
                                                                                                                                                                                                            
        if [ "$action" = "ON" ]; then                                                                                                                                                                       
                uci set wireless.default_radio0.ssid='SSID2'                                                                                                                                               
                uci set wireless.default_radio0.hidden='0'                                                                                                                                                  
                uci set wireless.default_radio1.ssid='SSID2'                                                                                                                                               
                uci set wireless.default_radio1.hidden='0'                                                                                                                                                  
                uci commit wireless                                                                                                                                                                         
                wifi reload                                                                                                                                                                                 
                logger -p notice -t wifi-toggle "SSID2"                                                                                                                                                    
        fi                                                                                                                                                                                                  
}

My system log only ever reads SSID2 regardless of what position I move the switch to. Wondering what could be causing this? Total newbie to coding in general too, so could be something super simple.

Any help would be awesome, Thanks :slight_smile:

Comparing the BTN_0 vs BTN_1 scripts, it seems one is very different to the other. This is stock though but I’m gonna put it here anyway in case it gives any clues

/etc/rc.button/BTN_0

#!/bin/sh

#This is the hidden button of AR300M

model=$(awk -F': ' '/machine/ {print tolower($NF)}' /proc/cpuinfo| cut -d- -f2-)
if [ "$model" = e750 ];then
        kill -9 $(pgrep -f "switch_queue")
        switch_queue &
else
        [ $SEEN -gt 0 ] || exit 0
        echo "$BUTTON ${ACTION}" > /dev/console
        /usr/bin/switchaction &
fi

/etc/rc.button/BTN_1

#!/bin/sh

[ $SEEN -gt 0 ] || exit 0

echo "$BUTTON ${ACTION}" > /dev/console

/usr/bin/switchaction &

~

It seems there’s no finish, and the echo phrase is exactly the same on each. Suggests to me why it isn’t working but its also the factory settings on these files…

after further research, when I log button presses in the BTN_0 and 1 files. BTN_1 never gets triggered. Could someone please show me how to fix the BTN_1 script?

Hmm my knowledge of bash/shell is limited.

I wonder if that is valid, don’t you need -eq to compare to strings?, it looks like you instance it which then always returns true.

Another thing… Linux is always very strict about case sensetivity you may also want to lowercase all the arguments in the if conditions which i persume is the action variable or is this more a enumeration?:thinking:

could be a good catch! would you mind providing some syntax for that? I don’t quite understand how it could be applied
I primarily used it as it was the same syntax that was already there.

seems like regardless of comparison, $1 always equals “ON” after reading the variable. It’s gotta be some problem where its not reading the switch value but I have no idea where

i would think of this:

#!/bin/sh                                                                                                                                                                                                   
. /lib/functions/gl_util.sh                                                                                                                                                                                 
                                                                                                                                                                                                            
toggle_tor(){                                                                                                                                                                                               
        local action=$1                                                                                                                                                                                     
        if [ "$action" -eq "off" ];then                                                                                                                                                                       
                tor_switch off                                                                                                                                                                              
        else                                                                                                                                                                                                
                tor_switch on                                                                                                                                                                               
        fi                                                                                                                                                                                                  
}                                                                                                                                                                                                           
                                                                                                                                                                                                            
toggle_wifi(){                                                                                                                                                                                              
        local action=$1                                                                                                                                                                                     
        if [ "$action" -eq "off" ]; then                                                                                                                                                                      
                uci set wireless.default_radio0.ssid='SSID1'                                                                                                                                               
                uci set wireless.default_radio0.hidden='1'                                                                                                                                                  
                uci set wireless.default_radio1.ssid='SSID1'                                                                                                                                               
                uci set wireless.default_radio1.hidden='1'                                                                                                                                                  
                uci commit wireless                                                                                                                                                                         
                wifi reload                                                                                                                                                                                 
                logger -p notice -t wifi-toggle "SSID1"                                                                                                                                                    
                                                                                                                                                                            
        fi                                                                                                                                                                                                  
        
# i wonder here to if you dont just want a else or elif here...                                                                                                                                                                                
        if [ "$action" -eq "on" ]; then                                                                                                                                                                       
                uci set wireless.default_radio0.ssid='SSID2'                                                                                                                                               
                uci set wireless.default_radio0.hidden='0'                                                                                                                                                  
                uci set wireless.default_radio1.ssid='SSID2'                                                                                                                                               
                uci set wireless.default_radio1.hidden='0'                                                                                                                                                  
                uci commit wireless                                                                                                                                                                         
                wifi reload                                                                                                                                                                                 
                logger -p notice -t wifi-toggle "SSID2"                                                                                                                                                    
        fi                                                                                                                                                                                                  
}

if its still case sensitive, you may need to use something like:

"$(action | tr '[:upper:]' '[:lower:]')" -eq "on"

Its difficult because bash is different than shell, and when you google you get stuff about bash shell :expressionless:

Interesting, I tried this approach to comparison, and it didn’t seem to come back true despite the value

toggle_wifi(){
        local action=$1
        logger -p notice -t action-variable-readout "$1"
        if [ "$action" -eq "ON" ]; then

the logger returns “ON” so this should be triggered. but nothing happens

and I have since tried an else statement there but the problem is it always returns “ON” regardless of physical switch location so I was hoping it would give me a better idea of what’s actually happening by assigning values

1 Like

From what i can read here unix - Compare a string using sh shell - Stack Overflow your code was correct with the = but instead of using "$action" it might need to be $(action) = "ON" can you check ?

Thank you for your help, I do really appreciate it. but it turns out it wasn’t the script that was the problem. I made a mistake following this guide; and wrote down toggle_wifi as equaling “ON” in both states… the upside of that is that my script does work, and I am not in fact crazy. For anyone following along this is the final (working) script.

toggle_wifi(){                                                                                                              
        local action=$1                                                                                                     
        logger -p notice -t action-variable-readout "$action"                                                               
        if [ "$action" = "ON" ]; then                                                                                       
                uci set wireless.default_radio0.ssid='SSID1'                                                               
                uci set wireless.default_radio0.hidden='1'                                                                  
                uci set wireless.default_radio1.ssid='SSID1'                                                               
                uci set wireless.default_radio1.hidden='1'                                                                  
                uci commit wireless                                                                                         
                wifi reload                                                                                                 
                logger -p notice -t wifi-toggle "SSID1"                                                                    
                                                                                                                            
        else                                                                                  
                uci set wireless.default_radio0.ssid='SSID2'                                                               
                uci set wireless.default_radio0.hidden='0'                                                                  
                uci set wireless.default_radio1.ssid='SSID2'                                                               
                uci set wireless.default_radio1.hidden='0'                                                                  
                uci commit wireless                                                                                         
                wifi reload                                                                                                 
                logger -p notice -t wifi-toggle "SSID2"                                                                    
        fi                                                                                                                  
}

and if you’re interesting in additional parameters for your ssid’s just type “uci show” in ssh and it will show you everything you can change. Marking as solved :pray:

2 Likes