-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwifi-autorun-on-connect.installer
executable file
·164 lines (134 loc) · 5.3 KB
/
wifi-autorun-on-connect.installer
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
#!/usr/bin/env bash
#########################################################################
# WiFi Autorun on Connect Installer #
# #
# Automatically run a script when connected to specific #
# WiFi networks. #
# #
# Part of HopeSeekr's BashScripts Collection #
# https://github.com/hopeseekr/BashScripts/ #
# #
# Copyright © 2020 Theodore R. Smith <[email protected]> #
# GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690 #
# #
# License: Creative Commons Attribution v4.0 International #
#########################################################################
# Require this script to be run as the root user.
# @see https://serverfault.com/a/37836/56309
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "Error: You *MUST* run this as sudo or root!"
exit 99
fi
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
echo "Usage: wifi-autorun-on-startup.installer [HOTSPOT] [SCRIPT]"
exit
fi
if [ ! -d "/etc/NetworkManager" ]; then
echo "Error: This script only works with NetworkManager." >&2
exit 2
fi
WIFI_HOTSPOT=""
if [ ! -z "$1" ]; then
if [ ! -f "/etc/NetworkManager/system-connections/${1}.nmconnection" ]; then
echo "Error: NetworkManager does not know about '${1}'." >&2
echo " Please try connecting to it first, retrying this script." >&2
exit 3
fi
WIFI_HOTSPOT="$1"
fi
if [ ! -z "$2" ] && [ ! -f "$2" ]; then
echo "Error: Cannot find a script at '${2}'." >&2
exit 4
fi
echo "Warning: This script only works with NetworkManager..."
echo ""
function selectHotspot() {
if [ -z "$WIFI_HOTSPOT" ]; then
echo "Known Hotspots:" >&2
HOTSPOTS=()
local index=0
local file
for file in /etc/NetworkManager/system-connections/*.nmconnection; do
index=$(( $index + 1 ))
filename=${file##*/system-connections/}
hotspot=${filename%%.nmconnection}
HOTSPOTS+=("$hotspot")
echo " ${index}) ${hotspot}" >&2
done
echo "" >&2
echo -n "Which hotspot (1-${index})? " >&2
read hotspot_pick
hotspot_pick=$(( $hotspot_pick - 1 ))
echo "${HOTSPOTS[$hotspot_pick]}"
else
echo $WIFI_HOTSPOT
fi
}
WIFI_HOTSPOT=$(selectHotspot)
echo "Chosen wifi network: ${WIFI_HOTSPOT}"
function assertScriptDoesNotExit()
{
SCRIPT_FILE="/etc/NetworkManager/dispatcher.d/on_connect-${1}.sh"
if [ -f "$SCRIPT_FILE" ]; then
echo "WARNING: A startup script for this connection already exists" >&2
echo " at '${SCRIPT_FILE}'." >&2
echo "" >&2
echo -n "Replace it or quit? [r/Q] " >&2
local replace_or_quit
read replace_or_quit
if [ "$replace_or_quit" != "r" ]; then
echo "Quitting..." >&2
exit 5
fi
fi
}
assertScriptDoesNotExit "${WIFI_HOTSPOT}"
function grabConnectionUUID()
{
local nm_conn_file="/etc/NetworkManager/system-connections/${1}.nmconnection"
if [ ! -f "$nm_conn_file" ]; then
echo "Error: NetworkManager does not know about '${1}'." >&2
echo " Please try connecting to it first, retrying this script." >&2
exit 3
fi
CONNECTION_UUID=$(grep uuid= "$nm_conn_file" | sed 's/^uuid=//')
echo "Connection UUID: $CONNECTION_UUID"
}
grabConnectionUUID "$WIFI_HOTSPOT"
#############################################################
# Grab the text from an existing file -or- user input... #
# #
# Copyright © 2020 Theodore R. Smith #
# License: Creative Commons Attribution v4.0 International #
# From: https://github.com/hopeseekr/BashScripts/ #
# @see https://stackoverflow.com/a/64486155/430062 #
#############################################################
function grabDocument()
{
if [ ! -z "$1" ] && [ -f "$1" ]; then
echo $(<"$1")
else
echo "" >&2
echo "Please type/paste in bash script you wish to be run when NetworkManager connects to '${WIFI_HOTSPOT}'." >&2
echo "You should NOT start with '#!/usr/bin/env bash'..." >&2
echo "Press CTRL+D when finished." >&2
echo "" >&2
# Read user input until CTRL+D.
# @see https://stackoverflow.com/a/38811806/430062
readarray -t user_input
# Output as a newline-dilemeted string.
# @see https://stackoverflow.com/a/15692004/430062
printf '%s\n' "${user_input[@]}"
fi
}
N=$'\n'
SCRIPT="#!/usr/bin/env bash${N}"
SCRIPT+="if [[ CONNECTION_UUID=\"${CONNECTION_UUID}\" ]]; then${N}"
SCRIPT+="$(grabDocument "$2")${N}"
SCRIPT+="fi${N}"
# Preserve white spaces and newlines.
# @see https://stackoverflow.com/a/18018422/430062
echo "$SCRIPT" > "$SCRIPT_FILE"
chmod 0755 "$SCRIPT_FILE"
echo ""
echo "Success! An autorun on connect script has been successfully installed for ${WIFI_HOTSPOT}."