Trigger a script on a network change in macosx
Each time the network is changed, the script locationchanger will be executed. In this sample, a rerouting is performed based on specific wifi connection. The users is prompt password in order to gain root privileges. Note that the password dialog message says something like "osascript wants to make changes” if you can change that, let me know...
If you wish to see a log of the script, edit locationchanger and set the logFile field.
To test it, simply stop and start the wifi or switch networks.
#! /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
<?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>