forked from rimar/wifi-location-changer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locationchanger
executable file
·110 lines (90 loc) · 3.17 KB
/
locationchanger
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
#!/bin/bash
# automatically change configuration of Mac OS X based on location
# author: Rocco Georgi <[email protected]>
# version: 0.4.0
# original author: Onne Gorter <[email protected]>
# url: http://tech.inhelsinki.nl/locationchanger/
# version: 0.4
# redirect all IO to a logfile
mkdir -p /usr/local/var/log
exec &>/usr/local/var/log/locationchanger.log
# to avoid any output, any logfile, uncomment the following:
#exec 1>/dev/null 2>/dev/null
# this service is called immediately after network activity.
# sleep a bit to make sure that info is finished writing to disk about those network changes
sleep 2
# determine location of this script
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# get SSID
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | awk -F ' SSID: ' '/ SSID:/ {print $2}'`
echo `date` "New SSID found: $SSID"
# empty location var
LOCATION=
# LOCATIONS
# (use to be used Location name here)
# =============================================
Location_Automatic="Automatic"
Location_Home="Home"
Location_Work="Company Intranet"
# SSIDS
# =====
SSID_TelekomPublic=Telekom
SSID_Home=HomeSSID
SSID_Work=WorkSSID
# SSID -> LOCATION mapping
case $SSID in
$SSID_TelekomPublic ) LOCATION="$Location_Automatic";;
$SSID_Home ) LOCATION="$Location_Home";;
$SSID_Work ) LOCATION="$Location_Work";;
# ... add more here
esac
REASON="SSID changed to $SSID"
# process mapping from text file
MAP_FILE=$SCRIPT_DIR/locationchanger.conf
if [ -f "$MAP_FILE" ]; then
# echo "Checking mappings from file \"$MAP_FILE\""
while IFS=' ' read -r loc wifi
do
if [[ "${loc}" == "#"* ]]; then
true # ignore comments
elif [ -z "${loc}" ]; then
true # ignore empty lines
else
wifi=$(echo $wifi | tr -d '"' ) # remove quotes
# echo "location, wifi: \"$loc\", \"$wifi\" "
if [[ "$wifi" == "$SSID" ]]; then
LOCATION="$loc"
REASON="SSID changed to $SSID using mapping file"
break
fi
fi
done < "$MAP_FILE"
fi
# still didn't get a location -> use Location_Automatic
if [ -z "$LOCATION" ]; then
LOCATION="$Location_Automatic"
REASON="Automatic Fallback"
fi
# Don't change if we're already there
current_location=$(networksetup -getcurrentlocation)
if [ "$current_location" = "$LOCATION" ]; then
exit
fi
# change network location: will output "found it!"
if ! networksetup -switchtolocation "$LOCATION"; then
osascript -e "display notification \"Failed to Change Network Location to: $LOCATION\" with title \"Network Update Failure\""
exit 1
fi
echo "" # add linefeed after output from networksetup
# if present, callout to an external script
# use this script's dir and also a file-naming convention to determine if there is an external script to be run
EXTERNAL_CALLOUT="$SCRIPT_DIR/locationchanger.callout.sh"
if [[ -x "$EXTERNAL_CALLOUT" ]]; then
echo "Calling external executable \"$EXTERNAL_CALLOUT\""
output=$($EXTERNAL_CALLOUT "$LOCATION")
exit_code=$?
echo "exit code: $exit_code and output: $output"
fi
osascript -e "display notification \"Network Location Changed to $LOCATION\" with title \"Network Update\""
echo "--> Location Changer: $LOCATION - $REASON"
exit 0