Created
July 14, 2014 11:28
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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