-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hotspot_switcher
executable file
·197 lines (174 loc) · 5.59 KB
/
hotspot_switcher
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
#! /bin/bash
#
# hotspot_switcher script to switch hotspot on/off
#
# Written by Nigel Bowden <wifinigel@gmail.com>.
#
# History:
#
# v0.01 - 25th July 2019 - Initial version based on Francois Verges' article
# v0.02 - 26th July 2019 - Added file checks before file copy/del operations
# v0.03 - 2nd Nov 2019 - Added bridging of interfaces
# v0.04 - 23rd Dec 2019 - Improved docs & added wlan adapter check
# v0.05 - 28th Jan 2020 - Added check for NICs that do not support AP mode
# v0.06 - 12th Jun 2020 - Added suport for new status file location
set -e
NAME=hotspot_switcher
DESC="Script to switch hotspot on/off"
VERSION=0.06
STATUS_FILE="/etc/wlanpi-state"
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
###############################################################################
#
# Activate hotspot:
#
# 1. Backup various existing files to allow restoration when hotspot
# deactivated
# 2. Remove a number of existing files that need to be replaced
# 3. Create links from deleted file locations to hotspot config files
# 4. Create status file to indicate hotspot is active
# 5. Reboot the wlanpi to ensure clean activation
#
###############################################################################
hotspot_on () {
echo "Starting switch from classic mode to hostpot mode"
# check what state the WLAN Pi is in classic mode
PI_STATUS=`cat $STATUS_FILE | grep 'classic'` || true
if [ -z "$PI_STATUS" ]; then
echo "Failed - WLAN Pi is not in classic mode."
exit 1
fi
# check if the WLAN NIC supports AP mode before switching
# iw list | awk '/Supported interface modes/, /Band/' | grep '\* AP'
AP_SUPPORT=`iw list | awk '/Supported interface modes/, /Band/' | grep '\* AP'` || true
if [ -z "$AP_SUPPORT" ]; then
echo "Failed - AP Mode not supported by adapter."
exit 1
fi
echo "Enabling hotspot..."
#Backup existing config files
echo "Backing up existing config files..."
cp /etc/default/isc-dhcp-server /etc/default/isc-dhcp-server.hspt
cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.hspt
cp /etc/network/interfaces /etc/network/interfaces.hspt
cp /etc/sysctl.conf /etc/sysctl.conf.hspt
cp /etc/default/ufw /etc/default/ufw.hspt
cp /etc/ufw/before.rules /etc/ufw/before.rules.hspt
# This file may or may not exist
if [ -e /etc/hostapd.conf ]; then
cp /etc/hostapd.conf /etc/hostapd.conf.hspt
fi
# Remove existing config files
echo "Removing existing config files..."
rm /etc/default/isc-dhcp-server
rm /etc/dhcp/dhcpd.conf
rm /etc/network/interfaces
rm /etc/sysctl.conf
rm /etc/default/ufw
rm /etc/ufw/before.rules
# This file may or may not exist
if [ -e /etc/hostapd.conf ]; then
rm /etc/hostapd.conf
fi
# Link to hotspot config files
echo "Creaing links to config files..."
ln -s /etc/wlanpihotspot/default/isc-dhcp-server /etc/default/isc-dhcp-server
ln -s /etc/wlanpihotspot/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf
ln -s /etc/wlanpihotspot/network/interfaces /etc/network/interfaces
ln -s /etc/wlanpihotspot/conf/hostapd.conf /etc/hostapd.conf
ln -s /etc/wlanpihotspot/sysctl/sysctl.conf /etc/sysctl.conf
ln -s /etc/wlanpihotspot/default/ufw /etc/default/ufw
ln -s /etc/wlanpihotspot/ufw/before.rules /etc/ufw/before.rules
# Signal that wconsole active
echo "hotspot" > $STATUS_FILE
echo "WLANPi will now reboot"
sleep 1
sync
reboot
}
###############################################################################
#
# Deactivate hotspot:
#
# 1. Remove links created during activation
# 2. Restore config files backed up during activation
# 3. Remove firewall rules added during activation
# 4. Remove status file to indicate hotspot no longer active
# 5. Reboot wlanpi to provide clean restoration of services
#
###############################################################################
hotspot_off () {
# check what state the WLAN Pi is in
PI_STATUS=`cat $STATUS_FILE | grep 'hotspot'` || true
if [ -z "$PI_STATUS" ]; then
echo "Failed - WLAN Pi is not in wconsole mode."
exit 1
fi
echo "Starting switch from hotspot mode to classic mode"
# Remove links to config files
echo "Removing links to config files..."
unlink /etc/default/isc-dhcp-server
unlink /etc/dhcp/dhcpd.conf
unlink /etc/network/interfaces
unlink /etc/hostapd.conf
unlink /etc/sysctl.conf
unlink /etc/default/ufw
unlink /etc/ufw/before.rules
# Restore original config files
echo "Restoring original config files..."
cp /etc/default/isc-dhcp-server.hspt /etc/default/isc-dhcp-server
cp /etc/dhcp/dhcpd.conf.hspt /etc/dhcp/dhcpd.conf
cp /etc/network/interfaces.hspt /etc/network/interfaces
cp /etc/sysctl.conf.hspt /etc/sysctl.conf
cp /etc/default/ufw.hspt /etc/default/ufw
cp /etc/ufw/before.rules.hspt /etc/ufw/before.rules
# This file may or may not exist
if [ -e /etc/hostapd.conf.hspt ]; then
cp /etc/hostapd.conf.hspt /etc/hostapd.conf
fi
echo "WLANPi will now reboot"
echo "classic" > $STATUS_FILE
sleep 1
sync
reboot
}
status () {
PI_STATUS=`cat $STATUS_FILE | grep 'hotspot'` || true
if [ -z "$PI_STATUS" ]; then
echo "hotspot is currently disabled"
exit 0
else
echo "hotpot is currently enabled"
exit 0
fi
}
version () {
N=/etc/wlanpihotspot/$NAME
echo "Version: $N $VERSION" >&2
exit 1
}
case "$1" in
on)
hotspot_on
;;
off)
hotspot_off
;;
status)
status
;;
install)
install
;;
version)
version;;
*)
N=/etc/wlanpihotspot/$NAME
echo "Usage: $N {on|off|status|version}" >&2
exit 1
;;
esac
exit 0