Wifi Preference Editing Script

jjones
Contributor II

Here's a little nugget that you all can enjoy :-)

I wrote this as we are switching our students to a different SSID, however the one they are on currently will still exist. This script changes the wifi preferred list to set the SSID you choose to the top. It also gives the option to remove a SSID of your choice. Another option this script has is to just remove a SSID rather move a SSID in the list. It has been tested on 10.10 and 10.11, it "should" work on 10.9 but I do not have a machine nearby to test it with.

The script has a check to ensure all settings are filled with input or it exits with what is missing. Here is a picture of the script options in Casper:db93d02778cd49f7bcd8cfe20314710f

~~

# /bin/bash

#This script is made to change the priority of the Wifi SSID's
#It has been tested and does not de-auth the user if they are currently on the SSID to be changed (If the SSID password remains in the keychain)
#This has been tested on 10.10 and 10.11

#Sets arguements for script
NetworkDevice=$4
PreferredNetwork=$5
WirelessSecurity=$6
RemoveNetworkAlso=$7
#Only fill NetworkDevice if using RemoveNetworkOnly. It will not run the other arguements
RemoveNetworkOnly=$8
#Setting argument to anything will trigger this to be run (i.e. asdfasdf) Make sure NetworkDevice is set. Will run with RemoveNetworkOnly
ShowPreferred=$9

#As stated above, if the RemoveNetworkOnly field is populated the script will only run the very last command.
if [ -z $RemoveNetworkOnly ]; then
#Checks if all arguements have been entered, will exit out with statement if no settings have been set.
    if [ -z $NetworkDevice ]; then
        echo "Please set the NetworkDevice"
        exit 1
    else
            if [ -z $PreferredNetwork ]; then
                echo "Please set the Preferred Network SSID"
                exit 1
            else
                    if [ -z $WirelessSecurity ]; then
                        echo "Please set the Wireless Security Type" 
                        exit 1
                    else        
                        #Echos all arguements
                        echo "Network Device is $NetworkDevice" 
                        echo "Preferred Network SSID is $PreferredNetwork" 
                        echo "Wireless Security Type is $WirelessSecurity" 

                        #Changes Priority of SSID to 0
                        networksetup -removepreferredwirelessnetwork "$NetworkDevice" "$PreferredNetwork"
                        networksetup -addpreferredwirelessnetworkatindex "$NetworkDevice" "$PreferredNetwork" 0 "$WirelessSecurity"

                            #If RemoveNetworkAlso is populated it will run otherwise the script will close
                            if [ -z $RemoveNetworkAlso ]; then
                                networksetup -removepreferredwirelessnetwork "$NetworkDevice" "$RemoveNetworkAlso"
                            else
                                echo Done

                            fi
                    fi
            fi
    fi
else
    if [ -z $NetworkDevice ]; then
        echo "Please set the NetworkDevice"
        exit 1
    else
        if [ -z $RemoveNetworkOnly ]; then
            echo "Unknown error, check to make sure script has not been edited"
            exit 1
        else
            networksetup -removepreferredwirelessnetwork "$NetworkDevice" "$RemoveNetworkOnly"
        fi
    fi
fi

#If arguement is filled with yes it will show preferred list *Warning can be a large list*
if [ -z $ShowPreferred ]; then
    echo Done
else
    networksetup -listpreferredwirelessnetworks "$NetworkDevice"
fi

exit 0

~~

9 REPLIES 9

jjones
Contributor II

Here is the updated script since the last post, it can now auto-detect which EN port is WiFi. I've also added the ability to change priority numbers (0 being top of list). Below is the setup for the script settings.

6f6de695a9194868aea1e3b61f74c2e3

# /bin/bash

#This script is made to change the priority of the populated SSID to 0 (Top of list)
#It has been tested and does not de-auth the user if they are currently on the SSID to be changed (If the SSID password remains in the keychain)
#This has been tested on 10.10 and 10.11

#Sets arguements for script
#Disabled NetworkDevice as NetworkPort will auto-detect WiFi port location
    Priority=$4
    PreferredNetwork=$5
    WirelessSecurity=$6
    RemoveNetworkAlso=$7
#Running RemoveNetworkOnly populated will not run other options
    RemoveNetworkOnly=$8
#Setting argument $9 to anything will trigger this to be run (i.e. asdfasdf) Will run with RemoveNetworkOnly
    ShowPreferred=$9
#Sets arguement to check for port location of wifi, nulls need of NetworkDevice.
    NetworkPort=`/usr/sbin/networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | cut -d' ' -f2`

#As stated above, if the RemoveNetworkOnly field is populated the script will only run the very last command.
if [ -z $RemoveNetworkOnly ]; then
#Checks if all arguements have been entered, will exit out with statement if no settings have been set.
    if [ -z $NetworkPort ]; then
        echo "WiFi Port not detected, please ensure system has WiFi capabilities"
        exit 1
    else
            if [ -z $PreferredNetwork ]; then
                echo "Please set the Preferred Network SSID"
                exit 1
            else
                    if [ -z $WirelessSecurity ]; then
                        echo "Please set the Wireless Security Type" 
                        exit 1
                    else        
                        #Echos all arguements
                        echo "Network Device is $NetworkPort" 
                        echo "Preferred Network SSID is $PreferredNetwork" 
                        echo "Wireless Security Type is $WirelessSecurity" 

                        #Changes Priority of SSID to 0
                        networksetup -removepreferredwirelessnetwork "$NetworkPort" "$PreferredNetwork"
                        networksetup -addpreferredwirelessnetworkatindex "$NetworkPort" "$PreferredNetwork" "$Priority" "$WirelessSecurity"
                        echo "Priority changed"

                            #If RemoveNetworkAlso is populated it will run otherwise the script will close
                            if [ -z $RemoveNetworkAlso ]; then
                                networksetup -removepreferredwirelessnetwork "$NetworkPort" "$RemoveNetworkAlso"
                            else
                                echo "SSID $RemoveNetworkAlso Removed"

                            fi
                    fi
            fi
    fi
else
    if [ -z $NetworkPort ]; then
        echo "WiFi Port not detected, please ensure system has WiFi capabilities"
        exit 1
    else
        if [ -z $RemoveNetworkOnly ]; then
            echo "Unknown error, check to make sure script has not been edited"
            exit 1
        else
            networksetup -removepreferredwirelessnetwork "$NetworkPort" "$RemoveNetworkOnly"
            echo "SSID Removed"
        fi
    fi
fi

#If arguement is populated it will show preferred list *Warning can be a large list*
#Do not rely fully on this as it can have false reporting, having a sleep time has not helped. 
if [ -z $ShowPreferred ]; then
    echo Done
else
    networksetup -listpreferredwirelessnetworks "$NetworkPort"
fi

exit 0

doctorj
New Contributor

You are the man!!! Thanks @jjones

jbjorn
New Contributor II

Script worked great to put our preferred wi-fi network atop users' network lists.

But, putting the preferred network into Parameter 5 and then the network to remove in Parameter 7 didn't seem to work -- it put the preferred network atop the list but didn't remove the other.

Creating a separate policy with the network to remove in Parameter 8 while leaving Parameters 4-7 blank did the trick.

cruess
New Contributor III

How about for iOS???

jonathan_rudge
New Contributor III

When running this script its actually removed my corporate entry and then re-added it without any security. What have I done wrong? This would remove all instances and make every device drop off the corporate network!

Script Results:

Running script Preferred Network SSID...
Script exit code: 0
Script result: /Library/Application Support/JAMF/tmp/Preferred Network SSID: line 31: [: WPA2: binary operator expected
Network Device is en0
Preferred Network SSID is "Corporate SSID"
Wireless Security Type is WPA2 Enterprise
Removed "Corporate SSID" from the preferred networks list
Security type is set to: Open
Added "Corporate SSID" to preferred networks list
Priority changed
Network was not found in the preferred networks list

tjgriffin
New Contributor III

I got the same thing.
If I set the Security type to WPA2 Enterprise, it just sets it to None.
If I set it as just WPA2, it defaults to WPA2 Personal.

Any ideas??

sdagley
Honored Contributor III

@iamgriffin See the script in the linked post which works with WPA2 Enterprise authenticated SSIDs (it sorts the SSID list directly rather than deleting and re-adding SSIDs as the script above does): Re-order WIFI Preferred Networks...

Dude
New Contributor

To set wireless encryption type use:
"WPA2E"

clegger06
New Contributor III

Thank you.