Very similar to this question, but as encouraged to do my @bmike I wanted to re-ask this question as it was not a duplicate of Create AirPort network from the command line.

I am looking for a programmatic way to connect to a wifi network. It can be saved (remembered) or unknown, preferably in Apple Script or via Automator.

I have done this on a Windows PC through the following command:

netsh wlan add profile wifi.xml user=current

(creating it, as the only available wifi network, would attempt to connect to it)

The real problem I am having is: I have a Mac Book, OSX 10.10, shared by two users. One user should be able to access (and preferably connect by default) to SSID "Wifi1" and the second user should be able to access SSID "Wifi2", but neither user should connect to the other user's wifi. The Windows netsh wlan with option user=current accomplishes this but I don't see this being supported on a Mac.

Disabling SSID remembering, and having the users type the password in each time, would of course accomplish this goal but I'm searching for a more automated solution.

I have not found a way to accomplish this via preferences, wifi priorities, remembering networks, or anything else.

Also to note, Fast User Switching is enabled (this appears to make scripting difficult in my research because there is no reliable trigger or event to listen to for the script to run).


This should do it, although it may need some modification. Every time a user switches, a system log entry is created. Knowing this, you can monitor for user switches quite effortlessly:

global wifi_device
on run
    set wifi_device to do shell script "networksetup -listallhardwareports|grep -A1 'Wi-Fi'|grep Device|sed 's;Device: ;;'"
end run
on idle
    set user_name to do shell script "tail -n0 -f /var/log/system.log|grep -om1 'context values set for .*'|sed -e 's;context values set for ;;'"
    if user_name is equal to "presaccount" then
        do shell script "networksetup -setairportnetwork " & wifi_device & " presaccount_network password"
    else if user_name is equal to "other_account" then
        do shell script "networksetup -setairportnetwork " & wifi_device & " other_account_network password"
    end if
    return 1
end idle

Just change the network names, passwords, and account names to whatever you need them to be.

Basically, we're checking for the log entry containing "context values set for", which occurs on every switch, and contains the username switched to. We use this username to determine which network to join, and that's it! I'd recommend only allowing admins to change the wifi network in system preferences. This actually uses nearly no resources, because the script cannot continue past the first line, until a user switches. Keep in mind that this was done on 10.11. I can't verify if the log entry is present on 10.10, so YMMV. You'll have to save this as a stay-open app in Script Editor.


My idea (based on this) is a bit different than William's.

It doesn't work with Fast User Switching but maybe one could use parts of William's stuff to mix it in.

This shell script should be placed in /usr/local/bin executable and owner root. The different locations (in Network Preferences) must have the names of the users and must be configured/populated. Also proper names of user1/2 and the SSID1/2 have to be set. It may also need some other improvements/modification.


# automatically change location and ssid based on user

# redirect all IO to /dev/null (comment this out if you want to debug)
# exec 1>/dev/null 2>/dev/null

# get a little breather before we get data for things to settle down
sleep 2

# get/set various system information
DEVICEEN="networksetup -listallhardwareports|grep -A1 'Wi-Fi'|grep Device|sed 's;Device: ;;'"

# basic pattern matching to get all location names separated by newlines
LOCATION_NAMES=`scselect | tail -n +2 | cut -d "(" -f 2 | cut -d ")" -f 1`
CURRENT_LOCATION=`scselect | grep " \* " | cut -d "(" -f 2 | cut -d ")" -f 1`

# no brackets as the grep will return a status code depending on whether the SSID has an own Location
if echo "$LOCATION_NAMES" | egrep -q "^$CURRENT_USER$"; then
  # auto does not seem to be working on Mac OS X Lion, but Automatic does
  if echo Automatic | grep -q "$LOCATION_NAMES"; then
  elif echo auto | grep -q "$LOCATION_NAMES"; then
    echo "Automatic location was not found!"
    echo "The following locations are known:"
    echo "$LOCATION_NAMES"

if [ "x$NEW_LOCATION" != "x" -a "x$NEW_LOCATION" != "x$CURRENT_LOCATION" ]; then
    echo "Changing to $NEW_LOCATION"
    scselect "$NEW_LOCATION"

if [ "$NEW_LOCATION" = "user1" ]; then
    echo "Changing to $SSID1"
    networksetup -setairportnetwork "$DEVICEEN" "$SSID1"
elif [ "$NEW_LOCATION" = "user2" ]; then
    echo "Changing to $SSID2"
    networksetup -setairportnetwork "$DEVICEEN" "$SSID2"
    echo "SSID was not found!"

The shell script has to be started by a launch agent plist in /Users/$User/Library/LaunchAgents.

Setting networksetup -setairportnetwork en1 "$SSID1/2"doesn't work properly for me. This might be solved by disabling SSID remembering and adding the Wi-Fi password in the networksetup... commands.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .