-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautolocation.sh
executable file
·291 lines (239 loc) · 7.12 KB
/
autolocation.sh
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
#=====================================================================
# autolocation.sh
#
# Detect which OS X Network Location to use based on Wireless SSID, IP
# addresses, or other means.
#
# Written by: Gregory Ruiz-Ade <gregory@ruiz-ade.com>
#
#---------------------------------------------------------------------
# LIMITATIONS:
#
# * Location names must be single-word strings due to not wanting to
# deal with bash quoting limitations.
#
# * Matching by IP address currently requires an exact match, I may
# figure out a way to do subnet matching in the future, like:
# - 10.0.1.0/24
# - 10.0.0.0/8
# It could be handy...
#---------------------------------------------------------------------
#
#---------------------------------------------------------------------
# Idea blatantly stolen from:
#
# Original author: Onne Gorter <o.gorter@gmail.com>
# url: http://tech.inhelsinki.nl/locationchanger/
#
# Modifications by Timothy Baldock <tb@entropy.me.uk>
#=====================================================================
#---------------------------------------------------------------------
# Locations which we should try to detect.
# Home
LOC_NAME[0]="Home"
LOC_SSID[0]="home_network"
LOC_EN0IP[0]="10.0.1.41"
LOC_EN1IP[0]="10.0.1.41"
# Work
#LOC_NAME[1]="Work"
#LOC_SSID[1]="work_network"
#LOC_EN0IP[1]=
#LOC_EN1IP[1]=
#---------------------------------------------------------------------
SELF=$(echo $0 | sed 's#^.*/##')
# log file
LOGFILE="${HOME}/.autolocation.log"
# Stuff all output into the log file (anything we don't shove through
# logger)
exec 1>>${LOGFILE} 2>&1
# Syslog facility/level to use for various conditions
ERRLOG="user.error"
WARNLOG="user.warn"
INFOLOG="user.info"
#---------------------------------------------------------------------
# Binaries that we need...
# logger(1)
LOGGER="/usr/bin/logger"
# scselect(8)
SCSELECT="/usr/sbin/scselect"
# growlnotify
GROWL="/usr/local/bin/growlnotify"
if [ ! -x $SCSELECT ] || [ ! -x $LOGGER ] || [ ! -x $GROWL ]; then
echo ""
echo "$SELF - FATAL"
echo "The following binaries are required and were not found:"
echo ""
echo " $SCSELECT"
echo " $LOGGER"
echo " $GROWL"
echo ""
echo "ABORTING."
echo ""
exit 1
fi
# Set some options we'll use throughout
# logger(1)
LOGGER_OPTS="-i -s -t ${SELF}"
LOGGER="${LOGGER} ${LOGGER_OPTS}"
#=====================================================================
# Functions
#=====================================================================
#---------------------------------------------------------------------
# get_ssid
#
# Usage:
#
# ssid=
# get_ssid ssid
# echo $ssid
function get_ssid ()
{
airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I"
ssid=$($airport 2>> >($LOGGER -p $ERRLOG) \
| grep ' SSID:' \
| cut -d ':' -f 2 \
| tr -d ' ')
eval "$1=$ssid"
}
#---------------------------------------------------------------------
# get_location
#
# Usage:
#
# location=
# get_location location
# echo $location
function get_location ()
{
# Grab the location, send any errors from scselect to logger
location=$($SCSELECT 2>> >($LOGGER -p $ERRLOG) \
| grep '^ \* ' \
| sed 's/.*(\(.*\))/\1/')
eval "$1=$location"
}
#---------------------------------------------------------------------
# set_location
#
# Usage
#
# set_location <location_name>
function set_location ()
{
# What's the requested location?
location=$1
# Log the activity
$LOGGER -p $INFOLOG "Setting Network Location to: $location"
# Set the location and send scselect output to logger
$SCSELECT $location 1>> >($LOGGER -p $INFOLOG) 2>> >($LOGGER -p $ERRLOG)
# If scselect failed, alert and bail.
if [ ${?} -ne 0 ]; then
$LOGGER -p $ERRLOG "Error selecting Location $location!"
$GROWL -s -p2 -n "Auto Location" \
-a "/Applications/Utilities/Airport Utility.app/" \
-m "ERROR: Unable to set Location: $location"
exit 1
fi
}
#=====================================================================
# Main
#=====================================================================
# Grab our current location
CUR_LOC=
get_location CUR_LOC
$LOGGER -p $INFOLOG "Current location is: $CUR_LOC"
# Get the associated SSID
SSID=
get_ssid SSID
if [ -n "$SSID" ]; then
$LOGGER -p $INFOLOG "Associated to SSID $SSID"
else
$LOGGER -p $INFOLOG "Not associated to any SSID"
fi
# Get the ethernet IP address
EN0IP=$(ifconfig en0 | grep -w inet | cut -d ' ' -f 2)
$LOGGER -p $INFOLOG "en0 IP: $EN0IP"
# Get the WiFi IP address
EN1IP=$(ifconfig en1 | grep -w inet | cut -d ' ' -f 2)
$LOGGER -p $INFOLOG "en1 IP: $EN1IP"
# Figure out which location we need to select based on the current
# network status.
NEW_LOC=
REASON=
i=0;
while [ $i -lt ${#LOC_NAME[@]} ]; do
if [ "$SSID" == "${LOC_SSID[$i]}" ]; then
NEW_LOC="${LOC_NAME[$i]}"
REASON="Matched WiFi"
break
elif [ -n "$EN0IP" && "$EN0IP" == "${LOC_EN0IP[$i]}" ]; then
NEW_LOC="${LOC_NAME[$i]}"
REASON="Matched en0 IP"
break
elif [ -n "$EN1IP" && "$EN1IP" == "${LOC_EN1IP[$i]}" ]; then
NEW_LOC="${LOC_NAME[$i]}"
REASON="Matched en1 IP"
break
fi
i=$(expr $i + 1)
done
# If we didn't find a configured location to match the current network
# state, fall back to Automatic.
if [ -z "$NEW_LOC" ]; then
NEW_LOC="Automatic"
REASON="No known locations detected"
fi
# At this point we have a decision as to what we want to do.
$LOGGER -p $INFOLOG "Selected $NEW_LOC as desired location."
# If the location isn't changing, do nothing.
if [ "$NEW_LOC" == "$CUR_LOC" ]; then
$LOGGER -p $INFOLOG "Location has not changed. Reason: $REASON"
exit 0
fi
# If the location is changing, figure out what we need to change it to
# and set the new location.
changed_loc=0
if [ "$NEW_LOC" == "Automatic" ]; then
# We're switching to Automatic
$LOGGER -p $INFOLOG "Setting Network Location $NEW_LOC"
set_location $NEW_LOC
changed_loc=1
else
# We're switching to a different location as defined up top.
i=0;
while [ $i -lt ${#LOC_NAME[@]} ]; do
if [ "$NEW_LOC" == "${LOC_NAME[$i]}" ]; then
$LOGGER -p $INFOLOG "Setting Network Location $NEW_LOC"
set_location $NEW_LOC
changed_loc=1
break
fi
i=$(expr $i + 1)
done
fi
# Check the current location again to see if it's changed
CUR_LOC=
get_location CUR_LOC
$LOGGER -p $INFOLOG "Current location is now: $CUR_LOC"
# If we think we've changed network locations...
if [ $changed_loc -eq 1 ]; then
# See if we really did change...
if [ "$CUR_LOC" == "$NEW_LOC" ]; then
# Yay, announce our success with pride!
$GROWL -s -p2 -n "Auto Location" \
-a "/Applications/Utilities/Airport Utility.app/" \
-m "Updated Location: $NEW_LOC Reason: $REASON SSID: $SSID"
$LOGGER -p $INFOLOG "Updated Location: $NEW_LOC; REASON: $REASON; SSID: $SSID"
else
# Boo, we completely failed, and need to tell someone.
$GROWL -s -p2 -n "Auto Location" \
-a "/Applications/Utilities/Airport Utility.app/" \
-m "FAILED! Unable to update Location to $NEW_LOC! Reason: $REASON SSID: $SSID"
$LOGGER -p $ERRLOG "FAILED! Unable to update Location to $NEW_LOC!"
fi
fi
# vim: ft=sh sw=4