Skip to content

Instantly share code, notes, and snippets.

@davidraviv
Created July 14, 2014 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidraviv/570c7ccee727d87aa681 to your computer and use it in GitHub Desktop.
Save davidraviv/570c7ccee727d87aa681 to your computer and use it in GitHub Desktop.
Trigger a script on a network change in macosx 1. Move locationchanger to a location of your wish and make it executable. 2. Edit LocationChanger.plist with the location of locationchanger. 3. Move LocationChanger to ~/Library/LaunchAgents/LocationChanger.plist 4. Execute the following on a terminal: launchctl load ~/Library/LaunchAgents/Locatio…
<?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>Label</key>
<string>locationchanger</string>
<key>ProgramArguments</key>
<array>
<string>/Users/davidraviv/bin/locationchanger</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration</string>
</array>
</dict>
</plist>
#! /bin/bash
#logFile = $HOME/bin/locationchanger.log
logFile = /dev/null
# reroute only when on mobimate wifi
# determine wifi ssid
ssid=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`
echo `date` Location: $ssid >> $logFile
if [ "$ssid" = "MyWifi" ]; then
# we are on MobiMate, need to reroute
echo `date` Rerouting... >> $logFile
# get sudo privileges and perform the rerouting
osascript -e "do shell script \"
route -nv add 10.0.0.0/8 10.200.10.150 >> $logFile
route -nv add 172.10.0.0/12 10.200.10.150 >> $logFile
\" with administrator privileges"
else
# we are on other wifi, no need to reroute
echo `date` No need to reroute >> $logFile
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment