public
Description: A launchd config and supporting scripts to watch for network location changes and execute a script if it finds a match.
Homepage:
Clone URL: git://github.com/UnderpantsGnome/location_watcher.git
location_watcher / location_watcher.sh
100755 111 lines (93 sloc) 3.08 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
 
# automatically change configuration of Mac OS X based on location
# author: Onne Gorter <o.gorter@gmail.com>
# url: http://tech.inhelsinki.nl/locationchanger/
# version: 0.4
#
# 2008-07-31 UnderpantsGnome (Michael Moen)
# url: http://github.com/UnderpantsGnome/location_watcher/wikis
# modified to call external scripts based on the location
# Made it easier to handle multiple locations
# made it read a config file in ~/.location_watcher
# added notifications or logging of activity/errors based on the config
 
# redirect all IO to /dev/null (comment this out if you want to debug)
exec 1>/dev/null 2>/dev/null
 
LOCK_FILE="HOME_DIR/.location_watcher.lock"
 
if [ ! -e ${LOCK_FILE} ]; then
touch ${LOCK_FILE}
  # get a little breather before we get data for things to settle down
  sleep 3
 
  # get various system information
  SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\
   | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' '`
  EN0IP=`ifconfig en0 | grep 'inet ' | cut -d' ' -f 2`
  EN1IP=`ifconfig en1 | grep 'inet ' | cut -d' ' -f 2`
 
  LOCATION='default'
  USE_NOTIFICATIONS="true"
 
  LOCATIONS=()
  SSIDS=()
  EN0IPS=()
  EN1IPS=()
 
  . HOME_DIR/.location_watcher
 
  message() {
    if [ "true" = "${USE_NOTIFICATIONS}" ]; then
if [ -e GROWL_PATH ]; then
GROWL_PATH -p ${2} -m "${1}" location_watcher ${3}
      else
osascript -l AppleScript -e "tell Application \"Finder\" to display alert \"location_watcher $1\""
      fi
else
log "${1}"
    fi
  }
 
  log() {
    echo "${1}" >> HOME_DIR/.location_watcher.log
  }
 
  for (( i = 0 ; i < ${#LOCATIONS[@]} ; i++ )); do
if [ "${SSID}" = "${SSIDS[$i]}" ]; then
REASON=${SSID}
      LOCATION=${LOCATIONS[$i]}
      break
fi
 
if [[ ("${EN0IP}" != "") && ("${EN0IP}" = "${EN0IPS[$i]}") ]]; then
REASON=${EN0IP}
      LOCATION=${LOCATIONS[$i]}
      break
fi
 
if [[ ("${EN1IP}" != "") && ("${EN1IP}" = "${EN1IPS[$i]}") ]]; then
REASON=${EN1IP}
      LOCATION=${LOCATIONS[$i]}
      break
fi
done
 
if [ ${LOCATION} ]; then
SCRIPT="HOME_DIR/bin/location_watcher/${LOCATION}"
    COMMON_SCRIPT="HOME_DIR/bin/location_watcher/common"
    touch HOME_DIR/.location_watcher.last
    LAST=`cat HOME_DIR/.location_watcher.last`
 
    if [ "${LOCATION}" != "${LAST}" ]; then
if [ -f "${SCRIPT}" ]; then
if [ -x "${SCRIPT}" ]; then
          $SCRIPT
          message "executed ${SCRIPT}" 1
          log "`date` Location: ${LOCATION} - ${REASON}"
          echo ${LOCATION} > HOME_DIR/.location_watcher.last
        else
message "${SCRIPT} exists, but it not executable" 2 -s
        fi
else
message "failed, ununable to find script, ${SCRIPT}" 2 -s
      fi
fi
 
if [ -f "${COMMON_SCRIPT}" ]; then
if [ -x "${COMMON_SCRIPT}" ]; then
        $COMMON_SCRIPT
        # message "executed ${COMMON_SCRIPT}" 1
      else
message "${COMMON_SCRIPT} exists, but it not executable" 2 -s
      fi
fi
fi
rm ${LOCK_FILE}
fi
exit 0