-
Notifications
You must be signed in to change notification settings - Fork 6
/
adhoc
executable file
·319 lines (286 loc) · 7.59 KB
/
adhoc
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash
#####################################
#Author: Leon Lee #
#email: lendylongli@gmail.com #
#QQ: 730395591 #
#Version: 1.0 #
#Note: If you have some GOOD ideas #
# or advice, please mail me^^ #
#Modified: flytreeleft @ 2012-09-27 #
# (flytreeleft@126.com) #
#####################################
#####################################
ShareNet=eth0
Wmode=ad-hoc
Wchannel=auto
Wessid=WiFi-Network
WInterface=$1
# the length of encrypted key is determined by device,
# running `sudo iwlist interface keys` to check
# the supported length
WPasskey=
Wkey=off
Igateway=192.168.2.254
Inetmask=255.255.255.0
Inetwork=192.168.2.0
DhcpRangeMin=192.168.2.10
DhcpRangeMax=192.168.2.105
#####################################
adhocFolder=/etc/adhoc
dnsmasqFile=$adhocFolder/dnsmasq.conf
resolvFile=/etc/resolv.conf
dnsmasqPid=/var/run/dnsmasq.pid
dnsmasqLeases=/var/run/dnsmasq.leases
# run command and print error message
function run_command
{
local command="$1"
local msg
msg=$(${command} 2>&1)
sleep 0.5
if [ $? -ne 0 ]; then
echo -e " [ \033[31mFAILED\033[0m ]"
echo " ${msg}"
exit 1
else
echo -e " [ GOOD ]"
fi
}
# start Ad-hoc
function adhoc_start
{
echo "Starting Ad-hoc..."
check_dnsmasq
sleep 0.5
echo -n "Setting $WInterface gateway[$Igateway] and netmask[$Inetmask]..."
run_command "ifconfig $WInterface $Igateway netmask $Inetmask"
echo -n "Setting down $WInterface..."
run_command "ifconfig $WInterface down"
# only is wlan0 shut down, you can set mode
echo -n "Setting $WInterface mode[$Wmode]..."
run_command "iwconfig $WInterface mode $Wmode"
echo -n "Setting $WInterface essid[$Wessid]..."
run_command "iwconfig $WInterface essid $Wessid"
echo -n "Setting $WInterface channel[$Wchannel]..."
run_command "iwconfig $WInterface channel $Wchannel"
if [ -n "$WPasskey" ]; then
Wkey="restricted"
echo -n "Setting $WInterface password[$WPasskey]..."
run_command "iwconfig $WInterface key s:$WPasskey"
else
Wkey="off"
fi
echo -n "Setting $WInterface key type[$Wkey]..."
run_command "iwconfig $WInterface key $Wkey"
if [ "$Wkey" = "off" ]; then
echo -e " [ \033[31mWARNING\033[0m, non-encrypted network ]"
fi
echo -n "Setting up $WInterface..."
run_command "ifconfig $WInterface up"
echo "Setting iptable:"
#remove the old rules
echo -n " remove old rules..."
iptables -N wireless-adhoc
iptables -F wireless-adhoc
iptables -t nat -F PREROUTING
iptables -t nat -F POSTROUTING
iptables -t nat -F
echo " [ GOOD ]"
#bring up the NAT rules
echo -n " bring up NAT rules..."
iptables -A wireless-adhoc -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A wireless-adhoc -s $Inetwork/24 -j ACCEPT
iptables -A wireless-adhoc -p 47 -j ACCEPT
iptables -A wireless-adhoc -j DROP
iptables -A FORWARD -m state --state INVALID -j DROP
iptables -A FORWARD -j wireless-adhoc
iptables -t nat -I POSTROUTING -s $Inetwork/24 -j MASQUERADE
echo " [ GOOD ]"
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
echo -n "Starting DNS and DHCP server..."
run_command "dnsmasq -i $WInterface \
--resolv-file=$resolvFile \
--conf-file=$dnsmasqFile"
echo "Completed!^^"
}
function wait_a_moment
{
sleep 0.5
echo -n "."
}
# stop Ad-hoc
function adhoc_stop
{
echo "Stopping adhoc ..."
sh -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
echo -n "."
# only is wlan0 shut down, you can set mode
ifconfig $WInterface down
wait_a_moment
# stop adhoc mode
iwconfig $WInterface mode managed
wait_a_moment
iwconfig $WInterface key off
wait_a_moment
iwconfig $WInterface essid any
wait_a_moment
# remove iptabled rules
iptables -D FORWARD -j wireless-adhoc
iptables -D FORWARD -m state --state INVALID -j DROP
iptables -F wireless-adhoc
iptables -X wireless-adhoc
iptables -t nat -F PREROUTING
iptables -t nat -F POSTROUTING
iptables -t nat -F
wait_a_moment
if [ -f $dnsmasqPid ]; then
dnsmasqID=`cat $dnsmasqPid`
kill $dnsmasqID
fi
if [ -f $dnsmasqLeases ]; then
rm $dnsmasqLeases
fi
echo -e "\nWifi ad-hoc now stopped"
}
function adhoc_restart
{
echo "Now, resart Ad-hoc ..."
adhoc_stop
sleep 1
adhoc_start
}
# check dnsmasq.conf
function check_dnsmasq
{
if [ -f $dnsmasqPid ]; then
echo "DHCP server is running!"
echo "Now, restart Ad-hoc"
adhoc_stop
fi
if [ ! -d $adhocFolder ]; then
mkdir $adhocFolder
fi
if [ ! -f $dnsmasqFile ]; then
echo "$dnsmasqFile is not exist, now building."
echo "dhcp-authoritative" > $dnsmasqFile
echo "dhcp-range=$DhcpRangeMin,$DhcpRangeMax,12h" >> $dnsmasqFile
echo "dhcp-leasefile=$dnsmasqLeases" >> $dnsmasqFile
echo "pid-file=$dnsmasqPid" >> $dnsmasqFile
echo "user=root" >> $dnsmasqFile
echo "no-negcache" >> $dnsmasqFile
fi
}
# is super user?
function super_user
{
if [ "$UID" = "0" ]; then
return 0
else
return 1
fi
}
# check whether encrypted key has supported length or not
function check_key_size
{
local key=$1
if [[ -n $key ]]; then
local key_bit_sizes=$(iwlist ${WInterface} keys \
| grep "sizes" \
| sed "s/.*: *\(.*\)bits/\1/; s/,/ /")
local key_byte_sizes
local wrong_key=1
local size
for size in ${key_bit_sizes}; do
key_byte_sizes="${key_byte_sizes},$((size/8))"
if ((${#key} == ${size}/8)); then
wrong_key=0
fi
done
if ((${wrong_key})); then
echo "Encrypted key must have ${key_byte_sizes#,} characters."
exit 1
fi
fi
}
# check whether the specified wifi interface is valid or not
function check_wifi_interface
{
local wifi="$1"
local interfaces=$(iwconfig 2> /dev/null \
| grep "ESSID" \
| sed "s/^\([^ ]\{1,\}\).*/\1/")
if [ -z "${interfaces}" ]; then
echo "It seems that you haven't any WiFi device!"
exit 1
fi
local inf
for inf in ${interfaces}; do
if [ "${wifi}" = "${inf}" ]; then
return 0
fi
done
echo "Maybe \"${wifi}\" is not a WiFi device which you have."
echo "Detected WiFi devices following:"
for inf in ${interfaces}; do
echo -n " ${inf}"
done
echo
exit 1
}
function usage
{
local program=$(echo $0 | sed "s/.*\/\([^\/]*\)$/\1/")
echo " Wifi Ad-hoc Control"
echo
echo "Usage:"
echo " ${program} interface [essid X] [key K] (start|stop|restart)"
echo
echo "Description:"
echo " essid The name of your network, default \"$Wessid\"."
echo " key The encryption key of your network,"
echo " you can use any ASCII string,"
echo " if not set, your network will be non-encrypted."
echo " start Start creating Wifi Ad-hoc Network."
echo " stop Stop Wifi Ad-hoc Network."
echo " restart Restart creating Wifi Ad-hoc Network."
}
if [ $# -lt 1 ]; then
usage
exit 1
fi
if ! super_user ; then
echo "Need super user permission!"
exit 1
fi
check_wifi_interface "$WInterface"
shift
while [[ -n $1 ]]; do
case $1 in
essid) shift
Wessid=$1
;;
key) shift
WPasskey=$1
check_key_size "${WPasskey}"
;;
stop) Action="stop"
;;
restart) Action="restart"
;;
start) Action="start"
;;
*) usage
exit 1
;;
esac
shift
done
case $Action in
stop) adhoc_stop
;;
restart) adhoc_restart
;;
start) adhoc_start
;;
*) usage
esac