Skip to content

Instantly share code, notes, and snippets.

@Stevearzh
Created September 20, 2013 17:13
Show Gist options
  • Save Stevearzh/6640728 to your computer and use it in GitHub Desktop.
Save Stevearzh/6640728 to your computer and use it in GitHub Desktop.
Open AP on Linux
#!/bin/bash
###########################################
#This shell is modified from Leon Lee, or-#
#iginal version see here: #
# #
#https://gist.github.com/anonymous/1117004#
# #
# #
#Notice: To run this shell, you should in-#
#stall hostapd and dnsmasq first. #
# #
#For example(archlinux): #
#sudo pacman -S hostapd dnsmasq #
# #
#If you have any question, please mail to #
#[email protected]. #
###########################################
ShareNet=enp7s0
WInterface=wlp8s0
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
###########################################
AP_Folder=/etc/AP
hostapdFile=/etc/hostapd/hostapd.conf
dnsmasqFile=$AP_Folder/dnsmasq.conf
resolvFile=/etc/resolv.conf
dnsmasqPid=/var/run/dnsmasq.pid
dnsmasqLeases=/var/run/dnsmasq.leases
############################################
# start AP
function AP_start
{
echo "Starting AP..."
check_dnsmasq
sleep 1
sudo ifconfig $WInterface down
echo -n "Setting $WInterface gateway and netmask."
sudo ifconfig $WInterface $Igateway netmask $Inetmask
sleep 1
echo
echo -n "Setting up $WInterface ..."
sudo ifconfig $WInterface up
sudo hostapd $hostapdFile -B
sleep 1
echo
echo -n "Setting iptable..."
sleep 1
#remove the old rules
sudo iptables -N wireless-AP
sudo iptables -F wireless-AP
sudo iptables -t nat -F PREROUTING
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -F
#bring up the NAT rules
sudo iptables -A wireless-AP -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A wireless-AP -s $Inetwork/24 -j ACCEPT
sudo iptables -A wireless-AP -p 47 -j ACCEPT
sudo iptables -A wireless-AP -j DROP
sudo iptables -A FORWARD -m state --state INVALID -j DROP
sudo iptables -A FORWARD -j wireless-AP
sudo iptables -t nat -I POSTROUTING -s $Inetwork/24 -j MASQUERADE
sleep 1
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
echo -n "."
sleep 1
sudo dnsmasq -i $WInterface --resolv-file=$resolvFile --conf-file=$dnsmasqFile
echo
echo "Success"
}
# stop AP
function AP_stop
{
echo "Stopping AP ..."
sudo sh -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
echo -n "."
sudo ifconfig $WInterface down
sudo pkill hostapd
sleep 1
# remove iptabled rules
sudo iptables -D FORWARD -j wireless-AP
sudo iptables -D FORWARD -m state --state INVALID -j DROP
sudo iptables -F wireless-AP
sudo iptables -X wireless-AP
sudo iptables -t nat -F PREROUTING
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -F
sleep 1
if [ -f $dnsmasqPid ]; then
dnsmasqID=`cat $dnsmasqPid`
kill $dnsmasqID
sleep 1
fi
if [ -f $dnsmasqLeases ]; then
rm $dnsmasqLeases
fi
echo
echo "Wifi AP now stopped"
}
# restart AP
function AP_restart
{
echo "Now, resart AP ..."
AP_stop
sleep 2
AP_start
}
# check dnsmasq.conf
function check_dnsmasq
{
if [ -f $dnsmasqPid ]; then
echo "Dhcp is running!"
echo "Now, restart AP"
AP_stop
sleep 1
fi
if [ ! -d $AP_Folder ]; then
mkdir $AP_Folder
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
}
function usage
{
echo "Wifi AP control"
echo "$1 [-h][-s]"
echo "Default is start AP"
echo "-h show the usage"
echo "-s stop AP"
echo "-r restart AP"
}
if ! super_user ; then
echo "Need super user permission!"
exit 1
fi
if [ $# -lt 1 ]; then
AP_start
elif [ "$1" = "-h" ]; then
usage
elif [ "$1" = "-s" ]; then
AP_stop
elif [ "$1" = "-r" ]; then
AP_restart
else
echo "Input error!"
echo "$1 -h gives usage information."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment