-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstant-ap.sh
executable file
·278 lines (220 loc) · 6.27 KB
/
instant-ap.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
# interfaces setup
IN_IF=wlan0
OUT_IF=ppp0
IN_ADDR="10.0.0.1"
# hostapd setup
SSID="INSTANT-AP"
CHANNEL="1"
WPA2_PASSPHRASE="yourverysecurepassphrase" # leave blank for open ap
HOSTAPD_CONF="./hostapd.conf"
# dnsmasq setup
DHCP_RANGE_START="10.0.0.101"
DHCP_RANGE_STOP="10.0.0.150"
LEASE_TIME="12h"
NAMESERVER_A=8.8.8.8 # nameservers to send to the clients
NAMESERVER_B=8.8.4.4 # nameservers to send to the clients
DNSMASQ_CONF="./dnsmasq.conf"
# ----------------------------------------------------------------------
# First of all, some little checks
ID=$(id -u)
if [ $ID -ne 0 ] ; then echo -e "\nERROR: you must be root to run this script!\n" ; exit 1 ; fi
which dnsmasq > /dev/null ; if [ $? -ne 0 ] ; then echo -e "\nERROR: dnsmasq is needed to run this script!\n" ; exit 1 ; fi
which hostapd > /dev/null ; if [ $? -ne 0 ] ; then echo -e "\nERROR: hostapd is needed to run this script!\n" ; exit 1 ; fi
# Let's roll
start() {
# disable handling of wireless interface by NetworkManager
if [ -z "$(ps -e | grep networkmanager)" ]
then
nmcli nm wifi off &> /dev/null || nmcli radio wifi off &> /dev/null
CMD1_EX=$?
rfkill unblock wlan
CMD2_EX=$?
if [ $(( $CMD1_EX + $CMD2_EX )) -eq 0 ]
then
echo "- Interface $IN_IF is no longer managed by NetworkManager"
else
echo "ERROR: Unable to unlock interface $IN_IF (still managed by NetworkManager)"
fi
fi
# Initial wifi interface configuration
echo
echo "- Interface $IN_IF setup as $IN_ADDR"
ifconfig $IN_IF up $IN_ADDR netmask 255.255.255.0
sleep 2
# Enable ipv4 forwarding
echo
echo "- Enabling ipv4 forwarding in kernel."
sysctl -w net.ipv4.ip_forward=1
# Enable NAT
echo
echo "- Starting iptables (MASQUERADE all / in: $IN_IF / out: $OUT_IF)."
iptables --flush
iptables --table nat --flush
iptables --delete-chain
iptables --table nat --delete-chain
iptables --table nat --append POSTROUTING --out-interface $OUT_IF -j MASQUERADE
iptables --append FORWARD --in-interface $IN_IF -j ACCEPT
# Uncomment the line below if facing problems while sharing PPPoE
#iptables -I FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
# Start dnsmasq
if [ -z "$(ps -e | grep dnsmasq)" ]
then
# write conf file for dnsmasq
echo "interface=$IN_IF" > $DNSMASQ_CONF
echo "no-resolv" >> $DNSMASQ_CONF
echo "dhcp-range=$DHCP_RANGE_START,$DHCP_RANGE_STOP,$LEASE_TIME" >> $DNSMASQ_CONF
echo "server=$NAMESERVER_A" >> $DNSMASQ_CONF
echo "server=$NAMESERVER_B" >> $DNSMASQ_CONF
echo
echo "- Starting dnsmasq with the following setup:"
echo
grep -v "^#" $DNSMASQ_CONF
echo
dnsmasq -C $DNSMASQ_CONF
else
echo
echo "- ERROR: dnsmasq is already running!"
if [ ! -z "$(ps -e | grep dnsmasq | grep -i networkmanager)" ]
then
echo "INFO: dnsmasq is being summoned by NetworkManager, please comment out the line 'dns=dnsmasq' in NetworkManager conf file and restart it"
fi
fi
# Start hostapd
if [ -z "$(ps -e | grep hostapd)" ]
then
# write conf file for hostapd
echo "interface=$IN_IF" > $HOSTAPD_CONF
echo "ssid=$SSID" >> $HOSTAPD_CONF
echo "channel=$CHANNEL" >> $HOSTAPD_CONF
if [ ! -z $WPA2_PASSPHRASE ]
then
echo "auth_algs=1" >> $HOSTAPD_CONF
echo "wpa=2" >> $HOSTAPD_CONF
echo "wpa_key_mgmt=WPA-PSK" >> $HOSTAPD_CONF
echo "rsn_pairwise=CCMP" >> $HOSTAPD_CONF
echo "wpa_passphrase=$WPA2_PASSPHRASE" >> $HOSTAPD_CONF
fi
echo
echo "- Starting hostapd with the following setup:"
echo
grep -v "^#" $HOSTAPD_CONF
echo
hostapd -B $HOSTAPD_CONF
else
echo
echo "ERROR: hostapd is already running!"
echo
fi
}
# Bring the whole thing down
stop() {
echo
echo "- flushing iptables rules"
iptables --flush && iptables --table nat --flush
echo
echo "- disabling ipv4 forwarding"
sysctl -w net.ipv4.ip_forward=0 > /dev/null
echo
echo "- killing dnsmasq"
killall -9 dnsmasq
rm -f $DNSMASQ_CONF
echo
echo "- killing hostapd"
killall -9 hostapd
rm -f $HOSTAPD_CONF
echo
echo "- bringing down interface $IN_IF"
ifconfig $IN_IF down
ifconfig mon.$IN_IF down
echo
if [ -z "$(ps -e | grep networkmanager)" ]
then
nmcli nm wifi on &> /dev/null || nmcli radio wifi on &> /dev/null
echo "- giving back Networmanager control over interface $IN_IF"
fi
echo
}
# WTF?
status() {
echo
ifconfig | grep $OUT_IF > /dev/null
if [ $? -eq 0 ]
then
OUT_IF_SETUP=$(ifconfig $OUT_IF | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | sed -e 's/^ *//g')
if [ -z "$OUT_IF_SETUP" ]
then
echo "- KO: $OUT_IF is up, but *NOT* configured!"
echo
else
echo "- OK: $OUT_IF is up. Setup: $OUT_IF_SETUP"
echo
fi
else
echo "- KO: $OUT_IF is *NOT* up!"
echo
fi
ifconfig | grep $IN_IF > /dev/null
if [ $? -eq 0 ]
then
IN_IF_SETUP=$(ifconfig $IN_IF | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | sed -e 's/^ *//g')
if [ -z "$IN_IF_SETUP" ]
then
echo "- KO: $IN_IF is up, but *NOT* configured!"
echo
else
echo "- OK: $IN_IF is up. Setup: $IN_IF_SETUP"
echo
fi
else
echo "- KO: $IN_IF is *NOT* up!"
echo
fi
if [ -z "$(ps -e | grep dnsmasq)" ]
then
echo "- KO: dnsmasq is *NOT* running!"
echo
else
DNSMASQ_PID=$(pidof dnsmasq)
N_CLIENTS=$(iw dev $IN_IF station dump | grep -i station | wc -l)
echo "- OK: dnsmasq is running. Pid: $DNSMASQ_PID ($N_CLIENTS client(s) connected)"
echo
fi
if [ -z "$(ps -e | grep hostapd)" ]
then
echo "- KO: hostapd is *NOT* running!"
echo
else
HOSTAPD_PID=$(pidof hostapd)
echo "- OK: hostapd is running. Pid: $HOSTAPD_PID"
echo
fi
FWD_SETUP=$(sysctl net.ipv4.ip_forward | cut -d '=' -f 2 | tr -d ' ' )
if [ $FWD_SETUP -eq "1" ]
then
echo "- OK: kernel ipv4 forwarding is active."
echo
else
echo "- KO: kernel ipv4 forwarding is *NOT* active!"
echo
fi
echo "- iptables (nat / POSTROUTING) setup:"
echo
iptables -t nat -n -L POSTROUTING -v | tail --lines=+2
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac