Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 73 lines (61 sloc) 1.59 KB
#!/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 \
state --state NEW -j ACCEPT
sudo iptables -A FORWARD -m \
state --state 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 /etc/init.d/isc-dhcp-server start
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 \
state --state NEW -j ACCEPT
sudo iptables -D FORWARD -m \
state --state 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 /etc/init.d/isc-dhcp-server stop
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