-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign_wifi_based_on_location.sh
executable file
·64 lines (54 loc) · 1.18 KB
/
assign_wifi_based_on_location.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
#!/bin/bash
function show_help {
echo "Automatically assign WiFi network based on location"
echo "Usage: $0"
}
OPTIND=1
# quiet mode not used right now
QUIET_MODE=0
while getopts "h?q" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
q) QUIET_MODE=1
;;
esac
done
export location=`/usr/local/bin/whereami predict`
case $location in
studyroom)
ssid="weihome"
;;
dinningroom)
ssid="weihome"
;;
mainbedroom)
ssid="weihome_EXT"
;;
livingroom)
ssid="weihome_GARDEN"
;;
*)
ssid="weihome"
;;
esac
echo "Connecting to $ssid ..."
AIRPORT="en0"
WIFI_NETWORK_NAME="$ssid"
WIFI_PASSWORD="xxxxxxxx"
CURRENT_WIFI_NETWORK_NAME=`/usr/sbin/networksetup -getairportnetwork $AIRPORT | awk '{print $NF}'`
if [ "$CURRENT_WIFI_NETWORK_NAME" == "$WIFI_NETWORK_NAME" ];
then
echo "Already connected to $ssid!";
exit 0
fi
if /usr/sbin/networksetup -setairportnetwork $AIRPORT $WIFI_NETWORK_NAME $WIFI_PASSWORD | grep -i -a "Failed" ;
then
echo "Failed to connect, just restarting...";
/usr/sbin/networksetup -setairportpower $AIRPORT off
/usr/sbin/networksetup -setairportpower $AIRPORT on
sleep 1
fi
/usr/sbin/networksetup -getairportnetwork $AIRPORT