forked from WLAN-Pi/wlanpihotspot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotspot_switcher
executable file
·164 lines (150 loc) · 4.56 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
#! /bin/bash
#
# hotspot_switcher script to switch hotspot on/off
#
# Written by Nigel Bowden <[email protected]>.
#
# 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
set -e
NAME=hotspot_switcher
DESC="Script to switch hotspot on/off"
VERSION=0.03
STATUS_FILE="/etc/wlanpihotspot/hotspot.on"
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 () {
# 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
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
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
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
touch $STATUS_FILE
echo "WLANPi will now reboot"
sleep 1
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 () {
echo "Disabling hotspot..."
# Remove 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
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"
if [ -e "$STATUS_FILE" ]; then
rm $STATUS_FILE
fi
sleep 1
reboot
}
status () {
if [ -e "$STATUS_FILE" ]; then
echo "hotspot is currently enabled"
exit 0
else
echo "hotspot is currently disabled"
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