2023-06-13 10:33:04 -06:00
|
|
|
#!/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]}
|
2023-06-13 14:14:38 -06:00
|
|
|
while [ -L "$SOURCE" ]; do
|
|
|
|
DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)
|
2023-06-13 10:33:04 -06:00
|
|
|
SOURCE=$(readlink "$SOURCE")
|
2023-06-13 14:14:38 -06:00
|
|
|
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
|
2023-06-13 10:33:04 -06:00
|
|
|
done
|
2023-06-13 14:14:38 -06:00
|
|
|
DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)
|
2023-06-13 10:33:04 -06:00
|
|
|
|
2023-06-20 12:03:35 -06:00
|
|
|
if [[ -f "$DIR/../../config/nat-config.sh" ]]; then
|
|
|
|
. "$DIR/../../config/nat-config.sh"
|
2023-06-13 10:33:04 -06:00
|
|
|
else
|
2023-06-20 12:03:35 -06:00
|
|
|
echo "$DIR/../../config/nat-config.sh missing!"
|
2023-06-13 10:33:04 -06:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ==============================================================================
|
|
|
|
# Setup
|
|
|
|
|
|
|
|
# Must be run as root
|
|
|
|
if [ "$(id -u)" -ne 0 ]; then
|
2023-06-13 14:14:38 -06:00
|
|
|
echo 'This script must be run as root.' >&2
|
|
|
|
exit 1
|
2023-06-13 10:33:04 -06:00
|
|
|
fi
|
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
echo -e "# PREPARE ENVIRONMENT #"
|
|
|
|
|
2023-06-13 14:14:38 -06:00
|
|
|
# Reset interfaces
|
|
|
|
iptables -X
|
|
|
|
iptables -F
|
|
|
|
iptables -t nat -X
|
|
|
|
iptables -t nat -F
|
2023-06-13 16:04:51 -06:00
|
|
|
echo "Erased all iptables rules."
|
2023-06-13 14:14:38 -06:00
|
|
|
|
|
|
|
ifconfig $WLAN_IFACE down
|
|
|
|
ifconfig $WLAN_IFACE hw ether $(ethtool -P $WLAN_IFACE | awk '{print $3}')
|
|
|
|
ifconfig $WLAN_IFACE up
|
2023-06-13 16:04:51 -06:00
|
|
|
echo "Reset the $WLAN_IFACE WiFi interface."
|
2023-06-13 14:14:38 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
# 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."
|
|
|
|
|
2023-06-13 10:33:04 -06:00
|
|
|
# We only need to get the $WLAN_IFACE IP address and will copy it over to $ETH_IFACE later
|
2023-06-13 14:14:38 -06:00
|
|
|
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)
|
2023-06-13 10:33:04 -06:00
|
|
|
|
2023-06-13 11:14:53 -06:00
|
|
|
if $NON_INTERACTIVE; then
|
|
|
|
NON_INTERACTIVE_APT="-y"
|
|
|
|
else
|
|
|
|
NON_INTERACTIVE_APT=""
|
|
|
|
fi
|
|
|
|
|
2023-06-13 10:33:04 -06:00
|
|
|
# ==============================================================================
|
|
|
|
# Install stuff
|
|
|
|
|
2023-06-13 14:14:38 -06:00
|
|
|
echo -e "\n# INSTALL THINGS #"
|
2023-06-13 10:33:04 -06:00
|
|
|
|
2023-06-13 11:14:53 -06:00
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
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
|
2023-06-13 16:13:32 -06:00
|
|
|
echo "Copying files..."
|
2023-06-13 16:04:51 -06:00
|
|
|
mkdir -p "$INSTALL_DIR"
|
|
|
|
cp -r "$DIR"/* "$INSTALL_DIR"
|
2023-06-13 16:13:32 -06:00
|
|
|
chown -R root:root /opt/wlan2eth
|
2023-06-13 16:04:51 -06:00
|
|
|
else
|
|
|
|
echo "We are already in $INSTALL_DIR, not copying files."
|
|
|
|
fi
|
|
|
|
|
2023-06-13 16:13:32 -06:00
|
|
|
echo -e "Upgrading...\n"
|
2023-06-13 16:04:51 -06:00
|
|
|
|
2023-06-13 11:14:53 -06:00
|
|
|
apt-get update
|
|
|
|
apt-get upgrade $NON_INTERACTIVE_APT
|
|
|
|
|
|
|
|
echo -e "\n"
|
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
THINGS_TO_INSTALL="net-tools ethtool dnsmasq openssh-server"
|
2023-06-13 10:33:04 -06:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2023-06-13 11:14:53 -06:00
|
|
|
apt-get install $NON_INTERACTIVE_APT $THINGS_TO_INSTALL
|
2023-06-13 10:33:04 -06:00
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
echo -e "\nStarting dnsmasq..."
|
2023-06-13 14:14:38 -06:00
|
|
|
sudo systemctl stop dnsmasq
|
|
|
|
sudo systemctl disable dnsmasq
|
2023-06-13 10:33:04 -06:00
|
|
|
|
|
|
|
if ! $NON_INTERACTIVE; then
|
2023-06-13 16:04:51 -06:00
|
|
|
echo -e "\nGoing to replace networking with systemd-networkd."
|
2023-06-13 10:33:04 -06:00
|
|
|
read -p "Press ENTER to continue or CTRL+C to cancel..."
|
|
|
|
fi
|
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
apt-get autoremove --purge $NON_INTERACTIVE_APT ifupdown dhcpcd5 isc-dhcp-client isc-dhcp-common
|
2023-06-13 10:33:04 -06:00
|
|
|
|
|
|
|
echo -e "\n\nConnecting to WiFi..."
|
|
|
|
|
|
|
|
WPA_SUPP_FILE="/etc/wpa_supplicant/wpa_supplicant-$WLAN_IFACE.conf"
|
2023-06-13 14:14:38 -06:00
|
|
|
cat >"$WPA_SUPP_FILE" <<EOF
|
2023-06-13 10:33:04 -06:00
|
|
|
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
|
|
|
|
update_config=1
|
2023-06-13 14:14:38 -06:00
|
|
|
country=USifconfig $WLAN_IFACE
|
2023-06-13 10:33:04 -06:00
|
|
|
network={
|
|
|
|
ssid="$WIFI_SSID"
|
|
|
|
scan_ssid=1
|
|
|
|
key_mgmt=WPA-EAP
|
|
|
|
eap=PEAP
|
|
|
|
identity="$WIFI_USERNAME"
|
|
|
|
password="$WIFI_PWD"
|
|
|
|
phase1="peaplabel=0"
|
|
|
|
phase2="auth=MSCHAPV2"
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
chmod 600 "$WPA_SUPP_FILE"
|
|
|
|
echo "Created wpa_supplicant: $WPA_SUPP_FILE"
|
|
|
|
systemctl disable wpa_supplicant.service
|
|
|
|
systemctl stop wpa_supplicant.service
|
|
|
|
systemctl enable --now wpa_supplicant@$WLAN_IFACE.service
|
|
|
|
systemctl status --no-pager wpa_supplicant@$WLAN_IFACE.service
|
|
|
|
echo ""
|
|
|
|
ifconfig $WLAN_IFACE
|
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
echo -e "\n"
|
2023-06-13 10:33:04 -06:00
|
|
|
NET_CONF_FILE="/etc/systemd/network/08-$WLAN_IFACE.network"
|
2023-06-13 14:14:38 -06:00
|
|
|
cat >"$NET_CONF_FILE" <<EOF
|
2023-06-13 10:33:04 -06:00
|
|
|
[Match]
|
|
|
|
Name=$WLAN_IFACE
|
|
|
|
[Network]
|
|
|
|
IPForward=yes
|
|
|
|
DHCP=yes
|
|
|
|
EOF
|
2023-06-13 16:20:22 -06:00
|
|
|
echo "Created network config for the $WLAN_IFACE WiFi interface."
|
2023-06-13 16:04:51 -06:00
|
|
|
echo -e "Finishing systemd-networkd install...\n"
|
2023-06-13 11:14:53 -06:00
|
|
|
apt-get install $NON_INTERACTIVE_APT libnss-resolve
|
2023-06-13 10:33:04 -06:00
|
|
|
ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
|
2023-06-13 14:14:38 -06:00
|
|
|
systemctl enable --now systemd-resolved.service
|
|
|
|
systemctl restart systemd-networkd.service
|
2023-06-13 10:33:04 -06:00
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
# echo -e "\n\nInstall complete! Ignore any service errors for now..."
|
2023-06-13 10:33:04 -06:00
|
|
|
|
|
|
|
sed -i'' 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
|
2023-06-13 14:14:38 -06:00
|
|
|
sysctl -p /etc/sysctl.conf
|
2023-06-13 10:33:04 -06:00
|
|
|
echo "Set net.ipv4.ip_forward=1 in /etc/sysctl.conf"
|
|
|
|
|
2023-06-13 14:14:38 -06:00
|
|
|
mkdir -p /var/lib/dnsmasq/
|
|
|
|
echo "Created /var/lib/dnsmasq/"
|
2023-06-13 10:33:04 -06:00
|
|
|
|
2023-06-13 15:39:52 -06:00
|
|
|
# Set up the SSH server. The port number may change and we won't always
|
|
|
|
# know what it used to be, so just remove all lines starting with "Port "
|
|
|
|
sed -i '/^Port /d' /etc/ssh/sshd_config
|
|
|
|
echo "Port $ROUTER_SSH_PORT" >> /etc/ssh/sshd_config
|
|
|
|
systemctl restart ssh
|
2023-06-13 16:04:51 -06:00
|
|
|
systemctl enable --now ssh
|
2023-06-13 15:39:52 -06:00
|
|
|
echo "Set SSH server port to $ROUTER_SSH_PORT"
|
|
|
|
|
2023-06-13 16:04:51 -06:00
|
|
|
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!"
|