Skip to content

Instantly share code, notes, and snippets.

@dduportal
Last active November 29, 2018 21:15
Show Gist options
  • Save dduportal/92a3e46da099d5d17540a80cf960895e to your computer and use it in GitHub Desktop.
Save dduportal/92a3e46da099d5d17540a80cf960895e to your computer and use it in GitHub Desktop.
Pi3: Sharing WiFi connexion to a bridge (eth + wifi)
#!/bin/bash
set -e
set -u
set -x
WIFI_INTERFACE=wlan0
VIRT_WIFI_INTERFACE=wlan1
BRIDGE_INTERFACE=br0
ETH_INTERFACE=eth0
IP_NET_PREFIX=10.0.0
IP_NETMASK=255.255.255.0
# Install Deps
apt-get update
apt-get install -y --no-install-recommends \
bridge-utils \
crda \
dnsmasq \
haveged \
hostapd \
nmap \
rng-tools
# Set Wifi to good country
iw reg set BE
#Stop unused services/interfaces
service hostapd stop
service dnsmasq stop
# Clean and Stop interface or nothing
ip addr flush dev "${ETH_INTERFACE}" || :
ifdown "${ETH_INTERFACE}" || :
# Clean and Stop interface or nothing
ip addr flush dev "${BRIDGE_INTERFACE}" || :
ifdown "${BRIDGE_INTERFACE}" || :
# Create the "virtual" Wifi interface for access point
[ $(iw dev | grep "${VIRT_WIFI_INTERFACE}" | wc -l) -eq 1 ] || \
iw dev "${WIFI_INTERFACE}" interface add "${VIRT_WIFI_INTERFACE}" type __ap
# Set the Ethernet interface in manual mode
cat <<EOF >/etc/network/interfaces.d/${ETH_INTERFACE}
auto ${ETH_INTERFACE}
iface ${ETH_INTERFACE} inet manual
EOF
ifup "${ETH_INTERFACE}"
# Create the bridge and associate the Ethernet to this
cat <<EOF >/etc/network/interfaces.d/${BRIDGE_INTERFACE}
auto ${BRIDGE_INTERFACE}
iface ${BRIDGE_INTERFACE} inet static
address ${IP_NET_PREFIX}.1
netmask ${IP_NETMASK}
network ${IP_NET_PREFIX}.0
broadcast ${IP_NET_PREFIX}.255
bridge_ports ${ETH_INTERFACE}
EOF
ifup "${BRIDGE_INTERFACE}"
# Configure hostpad to serve AP to "interface=" and be added to "bridge"
cat <<EOF >> /etc/hostapd/hostapd.conf
ssid=PI3
wpa_passphrase=raspberry
interface=${VIRT_WIFI_INTERFACE}
bridge=${BRIDGE_INTERFACE}
country_code=DE
auth_algs=3
channel=6
driver=nl80211
hw_mode=g
max_num_sta=5
rsn_pairwise=CCMP
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
EOF
echo 'DAEMON_CONF="/etc/hostapd/hostapd.conf"' >> /etc/default/hostapd
# Configure dnsmasq
mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bkp
touch /etc/dnsmasq.conf
echo "interface=${BRIDGE_INTERFACE}" >> /etc/dnsmasq.conf
echo "dhcp-range=${IP_NET_PREFIX}.1,${IP_NET_PREFIX}.254,12h" >> /etc/dnsmasq.conf
# Enable NAT and forwarding
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -j MASQUERADE
# Start services
systemctl enable dnsmasq
systemctl restart dnsmasq
systemctl enable hostapd
systemctl restart hostapd
sleep 5
brctl show
ip addr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment