Last active
July 23, 2021 04:14
-
-
Save nicholasadamou/948ba1a5bf8b315f9c9116fd292520a6 to your computer and use it in GitHub Desktop.
Enabling Simultaneous AP and Managed Mode WiFi on Raspberry Pi (Raspbian Stretch)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#see: https://albeec13.github.io/2017/09/26/raspberry-pi-zero-w-simultaneous-ap-and-managed-mode-wifi/ | |
x="/etc/udev/rules.d/70-persistent-net.rules" | |
cat < "$x" <<- EOL | |
SUBSYSTEM=="ieee80211", ACTION=="add|change", ATTR{macaddress}=="b8:27:eb:ff:ff:ff", KERNEL=="phy0", \ | |
RUN+="/sbin/iw phy phy0 interface add ap0 type __ap", \ | |
RUN+="/bin/ip link set ap0 address b8:27:eb:ff:ff:ff" | |
EOL | |
sudo apt install -y dnsmasq hostapd | |
x="/etc/dnsmasq.conf" | |
cat < "$x" <<- EOL | |
interface=lo,ap0 | |
no-dhcp-interface=lo,wlan0 | |
bind-interfaces | |
server=8.8.8.8 | |
domain-needed | |
bogus-priv | |
dhcp-range=192.168.10.50,192.168.10.150,12h | |
EOL | |
echo "Type a 1-32 character SSID (name) for your network, then press [ENTER]:" | |
read ssid | |
pwd1="0" | |
pwd2="1" | |
until [ $pwd1 == $pwd2 ]; do | |
echo "Type a password to access your network, then press [ENTER]:" | |
read -s pwd1 | |
echo "Verify password to access your network, then press [ENTER]:" | |
read -s pwd2 | |
done | |
if [ $pwd1 == $pwd2 ]; then | |
echo "Password set. Edit $x to change." | |
fi | |
x="/etc/hostapd/hostapd.conf" | |
cat < "$x" <<- EOL | |
ctrl_interface=/var/run/hostapd | |
ctrl_interface_group=0 | |
interface=ap0 | |
driver=nl80211 | |
ssid=$ssid | |
hw_mode=g | |
channel=11 | |
wmm_enabled=0 | |
macaddr_acl=0 | |
auth_algs=1 | |
wpa=2 | |
wpa_passphrase=$pwd1 | |
wpa_key_mgmt=WPA-PSK | |
wpa_pairwise=TKIP CCMP | |
rsn_pairwise=CCMP | |
EOL | |
#see: https://askubuntu.com/a/832592/552275 | |
iw dev wlan0 scan | egrep "signal|SSID" | sed -e "s/\tsignal: //" -e "s/\tSSID: //" | awk '{ORS = (NR % 2 == 0)? "\n" : " "; print} | sort | |
echo "Type the 1-32 character SSID (name) of the network you choose, then press [ENTER]: | |
read SSID | |
echo "Type a password to access your choosen network, then press [ENTER]:" | |
read -s PSK | |
x="/etc/default/hostapd" | |
cat < "$x" <<- EOL | |
DAEMON_CONF="/etc/hostapd/hostapd.conf" | |
EOL | |
x="/etc/wpa_supplicant/wpa_supplicant.conf" | |
cat < "$x" <<- EOL | |
country=US | |
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev | |
update_config=1 | |
network={ | |
ssid="$SSID" | |
psk="$PSK" | |
id_str="AP" | |
} | |
EOL | |
x="/etc/network/interfaces" | |
cp "$x" "$x".bak | |
cat < "$x" <<- EOL | |
# interfaces(5) file used by ifup(8) and ifdown(8) | |
# Please note that this file is written to be used with dhcpcd | |
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' | |
# Include files from /etc/network/interfaces.d: | |
source-directory /etc/network/interfaces.d | |
auto lo | |
auto ap0 | |
auto wlan0 | |
iface lo inet loopback | |
allow-hotplug ap0 | |
iface ap0 inet static | |
address 192.168.10.1 | |
netmask 255.255.255.0 | |
hostapd /etc/hostapd/hostapd.conf | |
allow-hotplug wlan0 | |
iface wlan0 inet manual | |
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf | |
iface AP inet dhcp | |
EOL | |
x="/usr/local/bin/start-ap-managed-wifi.sh" | |
cat < "$x" <<- EOL | |
#!/bin/bash | |
sleep 30 | |
sudo ifdown --force wlan0 && sudo ifdown --force ap0 && sudo ifup ap0 && sudo ifup wlan0 | |
sudo sysctl -w net.ipv4.ip_forward=1 | |
sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 ! -d 192.168.10.0/24 -j MASQUERADE | |
sudo systemctl restart dnsmasq | |
EOL | |
sudo chmod +x "$x" | |
(crontab -l 2>/dev/null; @reboot "$x") | crontab - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the procedure for the newer systemd based Raspbian/Raspberry OS versions.
https://raspberrypi.stackexchange.com/questions/89803/access-point-as-wifi-router-repeater-optional-with-bridge/89804#89804