#!/bin/bash # https://raspberrypi.stackexchange.com/questions/88954/workaround-for-a-wifi-bridge-on-a-raspberry-pi-with-proxy-arp # https://www.willhaley.com/blog/raspberry-pi-wifi-ethernet-bridge/ # ============================================================================== # Config SOURCE=${BASH_SOURCE[0]} while [ -L "$SOURCE" ]; do DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd) SOURCE=$(readlink "$SOURCE") [[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE done DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd) if [[ -f "$DIR/config/config.sh" ]]; then source "$DIR/config/config.sh" else echo "$DIR/config/config.sh missing!" exit 1 fi # ============================================================================== # Setup # Must be run as root if [ "$(id -u)" -ne 0 ]; then echo 'This script must be run as root.' >&2 exit 1 fi echo -e "# PREPARE ENVIRONMENT #" # Reset interfaces iptables -X iptables -F iptables -t nat -X iptables -t nat -F echo "Erased all iptables rules." ifconfig $WLAN_IFACE down ifconfig $WLAN_IFACE hw ether $(ethtool -P $WLAN_IFACE | awk '{print $3}') ifconfig $WLAN_IFACE up echo "Reset the $WLAN_IFACE WiFi interface." while true; do WLAN_IFACE_IP=$(ip -4 -br addr show $WLAN_IFACE | grep -Po "\\d+\\.\\d+\\.\\d+\\.\\d+") if [ -n "${WLAN_IFACE_IP}" ]; then echo "Got it!" break fi echo "Waiting for $WLAN_IFACE to get an IP..." sleep 5 done # Stop dnsmasq and restore systemd-resolved so we can avoid any DNS issues systemctl stop dnsmasq echo "Stopped dnsmasq." systemctl start systemd-resolved echo "Started systemd-resolved." # We only need to get the $WLAN_IFACE IP address and will copy it over to $ETH_IFACE later WLAN_NETMASK=$(ip addr show $WLAN_IFACE | grep -w inet | awk '{print $2}' | cut -d'/' -f2) WLAN_NETMASK_CIDR=$(ip addr show $WLAN_IFACE | grep -w inet | awk '{print $2}' | cut -d'/' -f2) if $NON_INTERACTIVE; then NON_INTERACTIVE_APT="-y" else NON_INTERACTIVE_APT="" fi # ============================================================================== # Install stuff echo -e "\n# INSTALL THINGS #" INSTALL_DIR="/opt/wlan2eth" echo "Installing to $INSTALL_DIR" if [ "$DIR" != "$INSTALL_DIR" ]; then if ! $NON_INTERACTIVE; then echo -e "\nGoing to copy $DIR to $INSTALL_DIR" read -p "Press ENTER to continue or CTRL+C to cancel..." fi echo "Copying files..." mkdir -p "$INSTALL_DIR" cp -r "$DIR"/* "$INSTALL_DIR" chown -R root:root /opt/wlan2eth else echo "We are already in $INSTALL_DIR, not copying files." fi echo -e "Upgrading...\n" apt-get update apt-get upgrade $NON_INTERACTIVE_APT echo -e "\n" THINGS_TO_INSTALL="net-tools ethtool dnsmasq openssh-server" if ! $NON_INTERACTIVE; then echo "Going to install: $THINGS_TO_INSTALL" read -p "Press ENTER to continue or CTRL+C to cancel..." fi echo -e "\n" apt-get install $NON_INTERACTIVE_APT $THINGS_TO_INSTALL echo -e "\nStarting dnsmasq..." sudo systemctl stop dnsmasq sudo systemctl disable dnsmasq if ! $NON_INTERACTIVE; then echo -e "\nGoing to replace networking with systemd-networkd." read -p "Press ENTER to continue or CTRL+C to cancel..." fi apt-get autoremove --purge $NON_INTERACTIVE_APT ifupdown dhcpcd5 isc-dhcp-client isc-dhcp-common echo -e "\n\nConnecting to WiFi..." WPA_SUPP_FILE="/etc/wpa_supplicant/wpa_supplicant-$WLAN_IFACE.conf" cat >"$WPA_SUPP_FILE" <"$NET_CONF_FILE" <> /etc/ssh/sshd_config systemctl restart ssh systemctl enable --now ssh echo "Set SSH server port to $ROUTER_SSH_PORT" echo "" systemctl status --no-pager ssh echo "" echo -e "Installing the wlan2eth systemd service..." cp "$DIR/wlan2eth.service" /etc/systemd/system/ systemctl daemon-reload systemctl enable --now wlan2eth echo "" systemctl status --no-pager wlan2eth echo -e "\n==============\nDone!\nNow reboot!"