-
Notifications
You must be signed in to change notification settings - Fork 4
/
adhoc
executable file
·72 lines (61 loc) · 1.63 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
#!/bin/bash
eth=eth0
wlan=wlan0
key=adhoc
essid=adam
lock=/tmp/adhoc_lock
name=$(basename "$0")
if [[ "$#" = "1" && "$1" = "on" ]]
then
if [ -f "$lock" ]
then
echo Adhoc network is already on.
exit 1
else
#start the ad hoc server
sudo ifconfig $wlan down
sudo iwconfig $wlan mode ad-hoc
sudo iwconfig $wlan key s:$key
sudo iwconfig $wlan essid "$essid"
sudo ifconfig $wlan 192.168.10.1 up
#forwards the ad hoc network to the router
sudo iptables -A FORWARD -i $wlan -o $eth -s 192.168.10.0/24 \
-m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD \
-m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A POSTROUTING -t nat -j MASQUERADE
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
#start the dhcp server
sudo systemctl start isc-dhcp-server.service
sudo touch $lock
echo Adhoc network is turned on.
fi
elif [[ "$#" = "1" && "$1" = "off" ]]
then
if [ -f "$lock" ]
then
#stop the ad hoc server
sudo ifconfig $wlan down
sudo iwconfig $wlan mode managed
sudo iwconfig $wlan key off
sudo iwconfig $wlan essid any
#delete added iptables rules
sudo iptables -D FORWARD -i $wlan -o $eth -s 192.168.10.0/24 \
-m conntrack --ctstate NEW -j ACCEPT
sudo iptables -D FORWARD \
-m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -D POSTROUTING -t nat -j MASQUERADE
sudo sh -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
#stop the dhcp server
sudo systemctl stop isc-dhcp-server.service
sudo rm -f $lock
echo Adhoc network is turned off.
else
echo Adhoc network is already off.
exit 1
fi
else
echo "Usage: $name on | off"
exit 1
fi
exit 0