Can Mac OS X be setup to automatically configure its Airport interface with a static IP address when connected to a Wifi network with a certain name, and on all other Wifi networks use DHCP?

Currently I'm using the "Locations" feature of the "Network" section in System Preferences to manually switch between two IP address setups. I have a "my static IP" setup, and a default "DHCP" setup. However this requires entering System Preferences to change the Location drop-down each time when switching locations. Can this be automated?

Preferably this would be done without third-party software.

up vote 8 down vote accepted

This is not something that can be done simply through the OS.

The aforementioned Locations is one option, along with freeware Airport Location or MarcoPolo or paid Network Location. There is also an AppleScript called WiFiScriptor that could help.

However! you need not go into System Preferences->Network->[Location dropdown] . You can go through the Apple Menu->Location (about 6 options down)->[select the location you want]

-b

  • 1
    Thanks for the tip about switching locations from the Apple Menu. Although still manual, that feels at least a little better than going into System Preferences with multiple steps. – Markus Hallmann Jan 8 '11 at 21:03
  • Also thanks for confirming that is not (currently) possible to do it without third-party software. – Markus Hallmann Jan 8 '11 at 21:09
  • the Airport Location link seems to point to a spam website now – Nat Jan 5 '16 at 18:49

It is possible to do without any third party, see https://github.com/rimar/wifi-location-changer

  • 2
    I would just like to add that in order for this to work, your Location name should be the same as the SSID of the wifi network. The logic of the script is that it checks for a Location with a name similar to the SSID and switches to that. If there's no Location with that name, it defaults to auto/Automatic. – Rystraum Mar 6 '14 at 2:24
  • Also, I had to log out and log back in before it started working on Yosemite. – djule5 Apr 2 '15 at 12:22

A third-party, non-free solution which would probably work for you is Locations.

As suggested by this answer to a similar, but not identical, question on stackoverflow, you could create a script which runs at a regular interval. launchd will prevent it from running more than once.

Psuedocode:

  • get saved_wifi value from a file
  • set current_wifi to output of networksetup -getairportnetwork AirPort
  • if saved_wifi == current_wifi, exit
  • else run networksetup -switchtolocation {location} where {location} is the name of the location you want to switch to based on the wifi network found.

There might be a chicken and egg situation here with connecting to local wifi which doesn't provide a DHCP address while you're configured to expect an address from DHCP.

--

Update: found possible duplicate question which suggests an opensource solution: MarcoPolo

Introducing Location Changer

Here's a very promising script for changing network locations based on WiFi SSID:

It is created by Anton Eprev. You can view the code and more detailed documentation on GitHub:

Installation

It installs via command line:

$ curl -L https://github.com/eprev/locationchanger/raw/master/locationchanger.sh | bash

It will ask you for a root password to install locationchanger to /usr/local/bin directory.

That's it!

After that, if there is a network location whose name is identical to name (SSID) of the current WiFi network, it will automatically switch to the network location. Otherwise, it will switch to Automatic location. (BTW, if there's no location called Automatic, it complains in logs and exits.)

Location-specific script

If you want to run a script every time you connect to a specific WiFi network, then put those scripts in ~/.locations and name them after WiFi networks, e.g. ~/.locations/Corp Wi-Fi:

#!/usr/bin/env bash

# Require password immediately after sleep or screen saver begins
osascript -e 'tell application "System Events" to set require password to wake of security preferences to true'

And you might want to create ~/.locations/Automatic that will reset those changes in other locations:

#!/usr/bin/env bash

# Don’t require password immediately after sleep or screen saver begins
osascript -e 'tell application "System Events" to set require password to wake of security preferences to false'

Make sure to cast execution bits on scripts (chmod +x).

Location aliases

You can share a network location between multiple WiFi networks. For instance, if you have a wireless router which broadcasts on 2.4 and 5GHz bands at the same time, you can create a configuration file ~/.locations/locations.conf and put simple key-value pairs inside:

Home_WiFi_5GHz=Home_WiFi

Here keys are WiFi network names, and values are corresponding network locations. Join them with bare equal signs (=, with no spaces). In the above example, if the WiFi network Home_WiFi_5GHz is connected, it will switch to the network location Home_WiFi.

Notice that location aliases take priority over the same-name rule. In above example, only script ~/.locations/Home_WiFi will run when you connect to the WiFi network Home_WiFi_5GHz.

With location aliases, you don't even need to create extra network locations for matching names of WiFi networks.

Troubleshooting

You can view extensive logs with the following command:

$ tail -f ~/Library/Logs/LocationChanger.log

Sample output:

[2017-10-13 11:35] Connected to 'Unknown WiFi'
[2017-10-13 11:35] Location 'Unknown WiFi' was not found. Will default to 'Automatic'
[2017-10-13 11:35] Changing the location to 'Automatic'
CurrentSet updated to 6B593A12-C51B-3FF1-DE1D-87310F232147 (Automatic)
[2017-10-13 11:37] Connected to 'Home_WiFi_5GHz'
[2017-10-13 11:37] Will switch the location to 'Home_WiFi' (configuration file)
[2017-10-13 11:37] Changing the location to 'Home_WiFi'
CurrentSet updated to 7900D1E6-3820-50C6-882E-4F5K2BEF32ED (Home_WiFi)

Uninstallation

First, unload the launch agent:

launchctl unload ~/Library/LaunchAgents/LocationChanger.plist

Then, remove the binary, plist and log file:

rm ~/Library/LaunchAgents/LocationChanger.plist
rm ~/Library/Logs/LocationChanger.log
sudo rm /usr/local/bin/locationchanger

You can also remove configuration files and location-specific scripts inside ~/.locations:

rm -rf ~/.locations

Since the given answer is manual, here is a simple script that changes it automatically:

Create various locations in System Preferences > Network to reflect each of your WiFi networks.

Script: (/usr/bin/autolocation.sh) Put this in some location you prefer and change the SSID values in the script according to your scenario.

#!/bin/sh

#get the ssid of the network we are on
ssid=`ioreg -l -n AirPortDriver | grep 80211SSID | sed 's/|//g' | sed 's/"//g' | sed 's/  */ /g' | awk '{print $3}'`

if [ $ssid = "voicegear" ]
then
        location="Office / Home"
elif [ $ssid = "vijay" ]
then
        location="Office / Home"
elif [ $ssid = "SCTHO" ]
then
        location="Shriram Santhome"
else
        location="Automatic"
fi

#update the location
newloc=`/usr/sbin/scselect "${location}" | sed 's/^.*(\(.*\)).*$/\1/'`

echo "${newloc}"

This script will change the network location (settings) based on the SSID of the wireless network. Now, this has to be run whenever there is a change in network settings. We’ll create a launchd agent to do the same.

launchd plist: (~/Library/LaunchAgents/com.vg.netwatcher.plist)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Disabled</key>
        <false/>
        <key>Label</key>
        <string>com.vg.netwatcher</string>
        <key>Program</key>
        <string>/usr/bin/autolocation.sh</string>
        <key>LaunchEvents</key>
        <dict>
                <key>com.apple.notifyd.matching</key>
                <dict>
                        <key>com.apple.system.config.network_change</key>
                        <dict>
                                <key>Notification</key>
                                <string>com.apple.system.config.network_change</string>
                        </dict>
                </dict>
        </dict>
        <key>KeepAlive</key>
        <true/>
</dict>
</plist>

I created this file in ~/Library/LaunchAgents (inside my home dir) as I'm the only user using this machine. If you want it to be global, put it in /Library/LaunchAgents.

This file has to be owned by root and have 600 permissions:

sudo chown root Library/LaunchAgents/com.vg.netwatcher.pist
sudo chmod 600 Library/LauhchAgents/com.vg.netwatcher.plist

Once you logout/login or reboot the system, the launch agent will start will start working depending on where you have put the file (your home dir or global). But if you do not want to do that and want it to load now, run the following on the command line. You have to do it only once.

cd ~/Library/LaunchAgents
sudo launchctl load -w com.vg.netwatcher.plist

That’s it. Now your Mac’s IP will change automatically.

Note: Works on El Capitan too!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.