forked from paulbhart/toggleairport
-
Notifications
You must be signed in to change notification settings - Fork 32
/
toggleAirport.sh
executable file
·107 lines (84 loc) · 2.85 KB
/
toggleAirport.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
#!/bin/bash
#[email protected]:paulbhart/toggleairport.git
#originally from https://gist.github.com/albertbori/1798d88a93175b9da00b
# rate limiting, run at most every second
if [ -f "/var/tmp/prev_toggle_airport_run" ]; then
prev_toggle_airport_run=`cat /var/tmp/prev_toggle_airport_run`
prev_toggle_airport_run_po=$(($prev_toggle_airport_run + 1))
current=`date +%s`
if (( $prev_toggle_airport_run_po > $current )); then
exit 0
fi
fi
date +%s > /var/tmp/prev_toggle_airport_run
function set_airport {
new_status=$1
if [ $new_status = "On" ]; then
/usr/sbin/networksetup -setairportpower $air_name on
touch /var/tmp/prev_air_on
else
/usr/sbin/networksetup -setairportpower $air_name off
if [ -f "/var/tmp/prev_air_on" ]; then
rm /var/tmp/prev_air_on
fi
fi
}
function notify {
osascript -e "display notification \"$1\" with title \"Wi-Fi Toggle\""
}
# Set default values
prev_eth_status="Off"
prev_air_status="Off"
eth_status="Off"
# Grab the names of the adapters. We assume here that any ethernet connection name ends in "Ethernet"
eth_names=`networksetup -listnetworkserviceorder | sed -En 's/^\(Hardware Port: .*(Ethernet|LAN).* Device: (en[0-9]+)\)$/\2/p'`
air_name=`networksetup -listnetworkserviceorder | sed -En 's/^\(Hardware Port: (Wi-Fi|AirPort).* Device: (en[0-9]+)\)$/\2/p'`
# Determine previous ethernet status
# If file prev_eth_on exists, ethernet was active last time we checked
if [ -f "/var/tmp/prev_eth_on" ]; then
prev_eth_status="On"
fi
# Determine same for Wi-Fi status
# File is prev_air_on
if [ -f "/var/tmp/prev_air_on" ]; then
prev_air_status="On"
fi
# Check actual current ethernet status
for eth_name in ${eth_names}; do
if ([ "$eth_name" != "" ] && [ "`ifconfig $eth_name | grep "status: active"`" != "" ]); then
eth_status="On"
fi
done
# And actual current Wi-Fi status
air_status=`/usr/sbin/networksetup -getairportpower $air_name | awk '{ print $4 }'`
# Determine whether ethernet status changed
if [ "$prev_eth_status" != "$eth_status" ]; then
if [ "$eth_status" = "On" ]; then
set_airport "Off"
notify "Wired network detected. Turning Wi-Fi off."
else
set_airport "On"
notify "No wired network detected. Turning Wi-Fi on."
fi
# If ethernet did not change
else
# Check whether Wi-Fi status changed
# If so it was done manually by user
if [ "$prev_air_status" != "$air_status" ]; then
set_airport $air_status
# if [ "$air_status" = "On" ]; then
# notify "Wi-Fi manually turned on."
# else
# notify "Wi-Fi manually turned off."
# fi
fi
fi
# Update ethernet status
if [ "$eth_status" == "On" ]; then
touch /var/tmp/prev_eth_on
else
if [ -f "/var/tmp/prev_eth_on" ]; then
rm /var/tmp/prev_eth_on
fi
fi
exit 0