Skip to content

Instantly share code, notes, and snippets.

@khronos227
Created January 4, 2019 05:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khronos227/63c6f32baf218ab9563072d07977dba2 to your computer and use it in GitHub Desktop.
Save khronos227/63c6f32baf218ab9563072d07977dba2 to your computer and use it in GitHub Desktop.
script to setup wifi access point for raspbian os
#!/bin/bash
function backup () {
if [ -e $1 -a ! -e $1.bak ]; then
cp $1{,.bak}
fi
}
function restore () {
cp $1{.bak,}
}
if [ $(whoami) != "root" ]; then
echo "root authentication is required"
exit 1
fi
if [[ $# -lt 1 ]]; then
echo "Usage:"
echo " $0 passphrase [apName]"
exit 1
fi
APPASS="$1"
APSSID="rpi-develop-wifi"
if [[ $# -eq 2 ]]; then
APSSID=$2
fi
# install packages
apt-get update -y
apt-get install -y hostapd dnsmasq
# dhcpcd
backup /etc/dhcpcd.conf
restore /etc/dhcpcd.conf
echo "denyinterfaces wlan0" >> /etc/dhcpcd.conf
cat <<EOF > /etc/network/interfaces.d/wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 172.24.1.1
netmask 255.255.255.0
network 172.24.1.0
broadcast 172.24.1.255
EOF
systemctl daemon-reload
service dhcpcd restart
/etc/init.d/networking restart
# hostapd
cat <<EOF > /etc/hostapd/hostapd.conf
interface=wlan0
hw_mode=g
channel=10
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_passphrase=$APPASS
ssid=$APSSID
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
EOF
backup /etc/default/hostapd
restore /etc/default/hostapd
echo "DAEMON_CONF=\"/etc/hostapd/hostapd.conf\"" >> /etc/default/hostapd
# dnsmasq
backup /etc/dnsmasq.conf
restore /etc/dnsmasq.conf
cat <<EOF > /etc/dnsmasq.conf
interface=wlan0
listen-address=172.24.1.1
server=8.8.8.8
dhcp-range=172.24.1.2,172.24.1.50,12h
EOF
# sysctl
backup /etc/sysctl.conf
restore /etc/sysctl.conf
sed -i -r 's/#(net.ipv4.ip_forward)/\1/' /etc/sysctl.conf
echo 1 > /proc/sys/net/ipv4/ip_forward
# iptables
iptables -t nat -F
iptables -F
iptables -X
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
iptables-save > /etc/iptables.ipv4.nat
# rc.local
backup /etc/rc.local
restore /etc/rc.local
sed -i -r 's/^exit 0/iptables-restore < \/etc\/iptables.ipv4.nat\nexit 0/' /etc/rc.local
service hostapd restart
service dnsmasq restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment