Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 7 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

share|improve this answer
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. – Valko Sipuli Jan 8 '11 at 21:03
    
Also thanks for confirming that is not (currently) possible to do it without third-party software. – Valko Sipuli Jan 8 '11 at 21:09
    
the Airport Location link seems to point to a spam website now – Nat Jan 5 at 18:49

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

share|improve this answer
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

share|improve this answer

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!

share|improve this answer

You must log in to answer this question.

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