-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpistar-ap.service
executable file
·215 lines (202 loc) · 5.99 KB
/
pistar-ap.service
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
#########################################################
# #
# Pi-Star HostAPd Service Handler #
# #
# Written for Pi-Star (http://www.mw0mwz.co.uk/pi-star) #
# By Andy Taylor (MW0MWZ) #
# #
# Version 1.5 #
# #
#########################################################
# Service Config
DAEMON=hostapd
DAEMON_PATH=/usr/sbin/
DAEMON_OPTS=" -B -P /run/pistar-hostapd.pid /etc/hostapd/hostapd.conf"
PGREP=/usr/bin/pgrep
KILL=/bin/kill
SLEEP=/bin/sleep
DNSMASQDAEMON=dnsmasq
# Pre-flight checks...
test -x ${DAEMON_PATH}${DAEMON} || exit 0
test -f "/etc/hostapd/hostapd.conf" || exit 0
if [ -f "/etc/hostap.off" ]; then
exit 0
fi
# Check that WLAN0 exists
if [ ! -d "/sys/class/net/wlan0" ] && [ ! -d "/sys/class/net/wlan0_ap" ]; then
exit 0
fi
# Check if the Wireless network is UP, and die if it IS
if [ -d "/sys/class/net/wlan0" ]; then
if [ `cat /sys/class/net/wlan0/operstate` == "up" ]; then
exit 0
fi
fi
# Make sure we have a card that supports AP mode
if [ `grep -c -E '(ath5k |ath9k |ath9k_htc |b43 |b43legacy |brcmfmac |carl9170 |cw1200 |iwlwifi |libertas_tf |mac80211_hwsim |mwifiex |mwl8k |p54spi |p54usb |rt73usb |rt2500usb |rt2800usb |vt6656 |wil6210 |wl12xx |zd1211rw |dhd |xradio_wlan |8188eu )' /proc/modules` -eq "0" ]; then
exit 0
fi
# Some cards cannot be in AP mode *AND* Managed mode at the same time
if [ `grep -c -E '(rt2800usb )' /proc/modules` -gt "0" ]; then
ifconfig wlan0 down
${SLEEP} 2
fi
# Some cards need special attention
if [ `grep -c -E '(8188eu )' /proc/modules` -gt "0" ]; then
if [ ! -d "/sys/class/net/wlan0_ap" ]; then
mount -o remount,rw /
sed -i '/hwaddress ether b8:27:eb:00:00:01/d' /etc/network/interfaces
ifconfig wlan0 down
ip link set wlan0 name wlan0_ap
${SLEEP} 2
fi
fi
# FriendlyArm Nano Pi Neo Air
if [ `grep -c -E '(dhd )' /proc/modules` -gt "0" ]; then
if [ ! -d "/sys/class/net/wlan0_ap" ]; then
mount -o remount,rw /
sed -i '/hwaddress ether b8:27:eb:00:00:01/d' /etc/network/interfaces
ifconfig wlan0 down
rmmod dhd
modprobe dhd op_mode=2
ip link set wlan0 name wlan0_ap
${SLEEP} 2
fi
fi
# Orange Pi Zero XRadio Fixes
if [ `grep -c -E '(xradio_wlan )' /proc/modules` -gt "0" ]; then
if [ ! -d "/sys/class/net/wlan0_ap" ]; then
mount -o remount,rw /
sed -i '/hwaddress ether b8:27:eb:00:00:01/d' /etc/network/interfaces
${SLEEP} 2
fi
fi
# Autofix DNSMASQ
if [ `grep -c -E 'DNSMASQ_EXCEPT' /etc/default/dnsmasq` -eq "0" ]; then
mount -o remount,rw /
echo "DNSMASQ_EXCEPT=lo" >> /etc/default/dnsmasq
fi
case "$1" in
start)
if [ -d /boot/firmware ]; then
(sudo mount -o remount,rw / 2>/dev/null)
else
mount -o remount,rw /
fi
if [ $(grep /var/lib/misc /proc/mounts | wc -l) -eq 0 ]; then
mount -t tmpfs -o nodev,noatime,nosuid,mode=1777,size=16k tmpfs /var/lib/misc
fi
sysctl net.ipv4.ip_forward=1
iw dev wlan0 interface add wlan0_ap type __ap
${SLEEP} 4 && ifup wlan0_ap && ${SLEEP} 3
if [ `${PGREP} ${DAEMON}` ]; then
echo -e "$DAEMON is already running as PID "`$PGREP $DAEMON`
else
systemctl start hostapd
echo -e "$DAEMON started as PID "`$PGREP $DAEMON`
fi
if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
echo -e "$DNSMASQDAEMON is already running as PID "`$PGREP $DNSMASQDAEMON`
else
systemctl start dnsmasq
echo -e "$DNSMASQDAEMON started as PID "`$PGREP $DNSMASQDAEMON`
fi
if [ -d /boot/firmware ]; then
(sudo mount -o remount,ro / 2>/dev/null)
else
mount -o remount,ro /
fi
exit 0
;;
stop)
if [ -d /boot/firmware ]; then
(sudo mount -o remount,rw / 2>/dev/null)
else
mount -o remount,rw /
fi
sysctl net.ipv4.ip_forward=0
if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
echo -e "Killing $DNSMASQDAEMON PID "`$PGREP $DNSMASQDAEMON`
systemctl stop dnsmasq
else
echo -e "$DNSMASQDAEMON is not running"
fi
if [ `${PGREP} ${DAEMON}` ]; then
echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
systemctl stop hostapd
${SLEEP} 5
else
echo -e "$DAEMON is not running"
fi
ifdown wlan0_ap
iw dev wlan0_ap del
if [ -d /boot/firmware ]; then
(sudo mount -o remount,ro / 2>/dev/null)
else
mount -o remount,ro /
fi
exit 0
;;
restart)
if [ -d /boot/firmware ]; then
(sudo mount -o remount,rw / 2>/dev/null)
else
mount -o remount,rw /
fi
sysctl net.ipv4.ip_forward=0
if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
echo -e "Killing $DNSMASQDAEMON PID "`$PGREP $DNSMASQDAEMON`
systemctl stop dnsmasq
else
echo -e "$DNSMASQDAEMON is not running"
fi
if [ `${PGREP} ${DAEMON}` ]; then
echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
systemctl stop hostapd
${SLEEP} 5
else
echo -e "$DAEMON is not running"
fi
ifdown wlan0_ap
iw dev wlan0_ap del
${SLEEP} 2
sysctl net.ipv4.ip_forward=1
iw dev wlan0 interface add wlan0_ap type __ap
${SLEEP} 3 && ifup wlan0_ap && ${SLEEP} 2
if [ `${PGREP} ${DAEMON}` ]; then
echo -e "$DAEMON is already running as PID "`$PGREP $DAEMON`
else
systemctl start hostapd
echo -e "$DAEMON started as PID "`$PGREP $DAEMON`
fi
if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
echo -e "$DNSMASQDAEMON is already running as PID "`$PGREP $DNSMASQDAEMON`
else
systemctl start dnsmasq
echo -e "$DNSMASQDAEMON started as PID "`$PGREP $DNSMASQDAEMON`
fi
if [ -d /boot/firmware ]; then
(sudo mount -o remount,ro / 2>/dev/null)
else
mount -o remount,ro /
fi
exit 0
;;
status)
if [ `${PGREP} ${DAEMON}` ]; then
echo -e "$DAEMON is running as PID "`${PGREP} ${DAEMON}`
else
echo -e "$DAEMON is not running"
fi
if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
echo -e "$DNSMASQDAEMON is running as PID "`${PGREP} ${DNSMASQDAEMON}`
else
echo -e "$DNSMASQDAEMON is not running"
fi
exit 0
;;
*)
echo $"Usage: $0 {start|stop|status}"
exit 0
esac