Skip to content

Instantly share code, notes, and snippets.

@robsonke
Last active May 9, 2016 06:48
Show Gist options
  • Save robsonke/6a5416d93d14bc75bfde5f3a8b060c0f to your computer and use it in GitHub Desktop.
Save robsonke/6a5416d93d14bc75bfde5f3a8b060c0f to your computer and use it in GitHub Desktop.
A simple bash script switching osx locations based on the active wifi network
#! /bin/bash
#######################################################
## Simple bash script to change OSX network location ##
## based on the wifi network you're connected too. ##
## Author: Rob Sonke ##
#######################################################
#
# NOTE: Make sure you add this line to the /etc/sudoers file to allow
# this command without prompting for a password every time.
# yourusername ALL=(root) NOPASSWD: /usr/sbin/networksetup
#
# Constants defining the situation
HOME_WIFI='homewifiname'
WORK_WIFI='workwifiname'
HOME_LOCATION='Home'
WORK_LOCATION='Work'
WIFI_DEV='en0'
# Gather information
WIFI_NAME="$(networksetup -getairportnetwork $WIFI_DEV | sed s/'Current Wi-Fi Network: '//g)"
CURRENT_LOCATION="$(networksetup -getcurrentlocation)"
# Check and change location
if [ $HOME_WIFI = $WIFI_NAME ] && [ $CURRENT_LOCATION != $HOME_LOCATION ]
then
# we're home, change location
sudo networksetup -switchtolocation $HOME_LOCATION
echo 'Home sweet home!'
elif [ $WORK_WIFI = $WIFI_NAME ] && [ $CURRENT_LOCATION != $WORK_LOCATION ]
then
# we're at work, check if location is work too
sudo networksetup -switchtolocation $WORK_LOCATION
echo 'Rise and shine, a new working day has started.'
else
# else nothing, all settings are fine or you went to a scary new location
echo 'Nothing to do, we are all set.'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment