Skip to content

Instantly share code, notes, and snippets.

@azsde
Last active February 7, 2023 16:41
Show Gist options
  • Save azsde/d621cb10082189d5bbc8408f907bba1c to your computer and use it in GitHub Desktop.
Save azsde/d621cb10082189d5bbc8408f907bba1c to your computer and use it in GitHub Desktop.
AutoHostapdConfig
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Must be root"
exit
fi
if [[ $# -lt 2 ]];
then echo "Invalid input"
echo "Usage:"
echo "sudo $0 apName password"
exit
fi
# Install every required software
apt update
apt install hostapd
systemctl unmask hostapd
systemctl enable hostapd
apt install dnsmasq
DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent
echo "# Config added by AutoWifiAp.sh
interface wlan0
static ip_address=192.168.4.1/24
nohook wpa_supplicant" >> /etc/dhcpcd.conf
echo "# Config added by AutoWifiAp.sh
# Enable IPv4 routing
net.ipv4.ip_forward=1" > /etc/sysctl.d/routed-ap.conf
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
netfilter-persistent save
mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
echo "# Config added by AutoWifiAp.sh
# Listening interface
interface=wlan0
# Pool of IP addresses served via DHCP
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
# Local wireless DNS domain
domain=wlan
# Alias for this router
address=/gw.wlan/192.168.4.1
" > /etc/dnsmasq.conf
rfkill unblock wlan
echo "# Config added by AutoWifiAp.sh
country_code=FR
interface=wlan0
ssid=$1
hw_mode=g
channel=7
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=$2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
" > /etc/hostapd/hostapd.conf
echo "Done, please reboot."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment