Skip to main content
Question

Wifi Preference Editing Script

  • November 16, 2015
  • 9 replies
  • 16 views

Forum|alt.badge.img+7

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:

~~

1# /bin/bash
2
3#This script is made to change the priority of the Wifi SSID's
4#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)
5#This has been tested on 10.10 and 10.11
6
7#Sets arguements for script
8NetworkDevice=$4
9PreferredNetwork=$5
10WirelessSecurity=$6
11RemoveNetworkAlso=$7
12#Only fill NetworkDevice if using RemoveNetworkOnly. It will not run the other arguements
13RemoveNetworkOnly=$8
14#Setting argument to anything will trigger this to be run (i.e. asdfasdf) Make sure NetworkDevice is set. Will run with RemoveNetworkOnly
15ShowPreferred=$9
16
17#As stated above, if the RemoveNetworkOnly field is populated the script will only run the very last command.
18if [ -z $RemoveNetworkOnly ]; then
19#Checks if all arguements have been entered, will exit out with statement if no settings have been set.
20 if [ -z $NetworkDevice ]; then
21 echo "Please set the NetworkDevice"
22 exit 1
23 else
24 if [ -z $PreferredNetwork ]; then
25 echo "Please set the Preferred Network SSID"
26 exit 1
27 else
28 if [ -z $WirelessSecurity ]; then
29 echo "Please set the Wireless Security Type"
30 exit 1
31 else
32 #Echos all arguements
33 echo "Network Device is $NetworkDevice"
34 echo "Preferred Network SSID is $PreferredNetwork"
35 echo "Wireless Security Type is $WirelessSecurity"
36
37 #Changes Priority of SSID to 0
38 networksetup -removepreferredwirelessnetwork "$NetworkDevice" "$PreferredNetwork"
39 networksetup -addpreferredwirelessnetworkatindex "$NetworkDevice" "$PreferredNetwork" 0 "$WirelessSecurity"
40
41 #If RemoveNetworkAlso is populated it will run otherwise the script will close
42 if [ -z $RemoveNetworkAlso ]; then
43 networksetup -removepreferredwirelessnetwork "$NetworkDevice" "$RemoveNetworkAlso"
44 else
45 echo Done
46
47 fi
48 fi
49 fi
50 fi
51else
52 if [ -z $NetworkDevice ]; then
53 echo "Please set the NetworkDevice"
54 exit 1
55 else
56 if [ -z $RemoveNetworkOnly ]; then
57 echo "Unknown error, check to make sure script has not been edited"
58 exit 1
59 else
60 networksetup -removepreferredwirelessnetwork "$NetworkDevice" "$RemoveNetworkOnly"
61 fi
62 fi
63fi
64
65#If arguement is filled with yes it will show preferred list *Warning can be a large list*
66if [ -z $ShowPreferred ]; then
67 echo Done
68else
69 networksetup -listpreferredwirelessnetworks "$NetworkDevice"
70fi
71
72exit 0

~~

9 replies

Forum|alt.badge.img+7
  • Author
  • Contributor
  • 97 replies
  • December 3, 2015

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.

1# /bin/bash
2
3#This script is made to change the priority of the populated SSID to 0 (Top of list)
4#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)
5#This has been tested on 10.10 and 10.11
6
7#Sets arguements for script
8#Disabled NetworkDevice as NetworkPort will auto-detect WiFi port location
9 Priority=$4
10 PreferredNetwork=$5
11 WirelessSecurity=$6
12 RemoveNetworkAlso=$7
13#Running RemoveNetworkOnly populated will not run other options
14 RemoveNetworkOnly=$8
15#Setting argument $9 to anything will trigger this to be run (i.e. asdfasdf) Will run with RemoveNetworkOnly
16 ShowPreferred=$9
17#Sets arguement to check for port location of wifi, nulls need of NetworkDevice.
18 NetworkPort=`/usr/sbin/networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | cut -d' ' -f2`
19
20#As stated above, if the RemoveNetworkOnly field is populated the script will only run the very last command.
21if [ -z $RemoveNetworkOnly ]; then
22#Checks if all arguements have been entered, will exit out with statement if no settings have been set.
23 if [ -z $NetworkPort ]; then
24 echo "WiFi Port not detected, please ensure system has WiFi capabilities"
25 exit 1
26 else
27 if [ -z $PreferredNetwork ]; then
28 echo "Please set the Preferred Network SSID"
29 exit 1
30 else
31 if [ -z $WirelessSecurity ]; then
32 echo "Please set the Wireless Security Type"
33 exit 1
34 else
35 #Echos all arguements
36 echo "Network Device is $NetworkPort"
37 echo "Preferred Network SSID is $PreferredNetwork"
38 echo "Wireless Security Type is $WirelessSecurity"
39
40 #Changes Priority of SSID to 0
41 networksetup -removepreferredwirelessnetwork "$NetworkPort" "$PreferredNetwork"
42 networksetup -addpreferredwirelessnetworkatindex "$NetworkPort" "$PreferredNetwork" "$Priority" "$WirelessSecurity"
43 echo "Priority changed"
44
45 #If RemoveNetworkAlso is populated it will run otherwise the script will close
46 if [ -z $RemoveNetworkAlso ]; then
47 networksetup -removepreferredwirelessnetwork "$NetworkPort" "$RemoveNetworkAlso"
48 else
49 echo "SSID $RemoveNetworkAlso Removed"
50
51 fi
52 fi
53 fi
54 fi
55else
56 if [ -z $NetworkPort ]; then
57 echo "WiFi Port not detected, please ensure system has WiFi capabilities"
58 exit 1
59 else
60 if [ -z $RemoveNetworkOnly ]; then
61 echo "Unknown error, check to make sure script has not been edited"
62 exit 1
63 else
64 networksetup -removepreferredwirelessnetwork "$NetworkPort" "$RemoveNetworkOnly"
65 echo "SSID Removed"
66 fi
67 fi
68fi
69
70#If arguement is populated it will show preferred list *Warning can be a large list*
71#Do not rely fully on this as it can have false reporting, having a sleep time has not helped.
72if [ -z $ShowPreferred ]; then
73 echo Done
74else
75 networksetup -listpreferredwirelessnetworks "$NetworkPort"
76fi
77
78exit 0

Forum|alt.badge.img
  • New Contributor
  • 1 reply
  • March 14, 2016

You are the man!!! Thanks @jjones


Forum|alt.badge.img+4
  • New Contributor
  • 4 replies
  • October 31, 2016

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.


Forum|alt.badge.img+4
  • Contributor
  • 22 replies
  • January 6, 2017

How about for iOS???


Forum|alt.badge.img+5

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
Forum|alt.badge.img+4
  • Contributor
  • 11 replies
  • November 20, 2017

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
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3566 replies
  • November 20, 2017

@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...


Forum|alt.badge.img+2
  • New Contributor
  • 1 reply
  • August 8, 2018

To set wireless encryption type use:
"WPA2E"


Forum|alt.badge.img+4
  • Contributor
  • 15 replies
  • November 13, 2018

Thank you.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings