Mac OS X change IP configuration automatically by location (by Wifi network)?

Solution 1:

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

Solution 2:

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

Solution 3:

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

Solution 4:

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!

Solution 5:

Introducing Location Changer

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

  • http://eprev.org/2015/08/06/change-os-x-network-location-based-on-the-wi-fi-network-name/

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

  • https://github.com/eprev/locationchanger

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