Skip to content

Instantly share code, notes, and snippets.

@egll-tech
Last active December 14, 2023 14:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egll-tech/00a097355aac03d05a5a2a023ae48873 to your computer and use it in GitHub Desktop.
Save egll-tech/00a097355aac03d05a5a2a023ae48873 to your computer and use it in GitHub Desktop.
Make your Raspberry Pi share your WiFi connection through the ethernet port. Made from Jiab77 tutorial (https://gist.github.com/Jiab77/76000284f8200da5019a232854421564)
#!/bin/bash
# Check sudo rights
if [[ "$EUID" = 0 ]]; then
echo "Already root"
else
sudo -k # make sure to ask for password on next sudo
if sudo false; then
echo "Wrong password"
exit 1
fi
fi
bold=$(tput bold)
normal=$(tput sgr0)
# Message
echo "======================================= I M P O R T A N T ======================================="
echo "Please modify your network settings in ${bold}/etc/netplan/${normal} following this guidelines:"
echo "1) set ${bold}renderer${normal} to ${bold}networkd${normal} or ${bold}NetworkManager${normal}"
echo "2) set your ${bold}ethernet (eth0) dhcp4${normal} to ${bold}false${normal}"
echo "3) set your ${bold}wifi (wlan0) dhcp4${normal} to ${bold}true${normal}"
echo "4) remove ${bold}eth0${normal} and ${bold}wlan0${normal} from any bridge"
echo "4) execute ${bold}sudo netplan --debug try${normal}"
echo "5) execute ${bold}sudo netplan --debug generate${normal}"
echo "7) execute ${bold}sudo netplan --debug apply${normal}"
echo "-------------------------------------------------------------------------------------------------"
echo "In case you set ${bold}NetworkManager${normal} remember to set ${bold}/etc/default/crda${normal}"
echo "================================================================================================="
# Updating and installing packages
echo "Proceeding to update, upgrade and installing ${bold}parprouted dhcp-helper wireless-tools wpasupplicant avahi-daemon${normal}"
{
sudo apt update --fix-missing -y && sudo apt dist-upgrade -y && sudo apt autoremove --purge -y
sudo apt install wireless-tools wpasupplicant parprouted dhcp-helper avahi-daemon -y -f
} &> /dev/null &
PID=$!
i=1
sp="/-\|"
echo -n ' '
while [ -d /proc/$PID ]
do
printf "\b${sp:i++%${#sp}:1}"
done
printf "\b"
# Updating configuration files
sudo sed -i -e 's/eth0/wlan0/g' /etc/default/dhcp-helper
sudo sed -i -e 's/#enable-reflector=no/enable-reflector=yes/g' /etc/avahi/avahi-daemon.conf
# Generating service file
cat > /lib/systemd/system/arp-bridge.service <<EOL
[Unit]
Description=ARP Bridge over Wireless Interface
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/lib/systemd/system/set-arp-routing start
ExecStop=/lib/systemd/system/set-arp-routing stop
ExecReload=/lib/systemd/system/set-arp-routing restart
[Install]
WantedBy=multi-user.target
EOL
# Creating script
sudo curl -o /lib/systemd/system/set-arp-routing https://gist.githubusercontent.com/egll-tech/00a097355aac03d05a5a2a023ae48873/raw/set-arp-routing &> /dev/null
sudo chmod -v u+x /lib/systemd/system/set-arp-routing &> /dev/null
# Reload systemd config
sudo systemctl daemon-reload
# Enable and start the service
sudo systemctl enable --now arp-bridge.service
# Final message
echo "${bold}Please reboot your pi"
exit 0
#!/bin/bash
## ARP Routing Management Script
## Made by Jiab77 <jonathan.barda@gmail.com>
## Original post https://gist.github.com/Jiab77/76000284f8200da5019a232854421564
## Config
ETHERNET_IFACE=eth0
WIRELESS_IFACE=wlan0
KEYBOARD_LAYOUT=us
DISABLE_POWER_MGMT=true
## Help
if [ $# -eq 0 ]; then
echo -e "\nUsage: $0 action [start|stop|restart|status]\n"
exit
fi
## Functions
function start_service {
## Setup system forwarding
echo -e "\nEnable IP forwarding..."
echo 1 > /proc/sys/net/ipv4/ip_forward
## Uncomment lines below if you don't want to use parprouted.
#echo "Enable ARP forwarding"
# Comment for all interfaces
#echo 1 > /proc/sys/net/ipv4/conf/${WIRELESS_IFACE}/proxy_arp
# Uncomment for all interfaces
#echo 1 > /proc/sys/net/ipv4/conf/all/proxy_arp
## Fix the 'eth0' interface that don't want to mount at boot on 3b+
## Uncomment this part if the 'eth0' interface is not mounted at boot
#/usr/sbin/netplan apply
## A little sleep
sleep 2
## Assign address
echo "Cloning IP from ${WIRELESS_IFACE} to ${ETHERNET_IFACE}..."
/sbin/ip addr add $(/sbin/ip addr show $WIRELESS_IFACE | perl -wne 'm|^\s+inet (.*)/| && print $1')/32 dev $ETHERNET_IFACE
## Uncomment lines below in case you're encountering the same issues
#echo "Removing bad APIPA IP from ${ETHERNET_IFACE}"
#/sbin/ip addr del $(/sbin/ip addr show $ETHERNET_IFACE | perl -wne 'm|^\s+inet (169.254.*)/| && print $1')/16 dev $ETHERNET_IFACE
#echo "Removing bad APIPA route from ${ETHERNET_IFACE}"
#/sbin/ip route del 169.254.0.0/16 dev $ETHERNET_IFACE
## Make sure that the eth0 interface is up
echo "Setting up lan interface..."
/sbin/ip link set $ETHERNET_IFACE up
## Setup ARP forwarding
echo "Starting paraprouted..."
/usr/bin/killall -KILL parprouted 2> /dev/null
/usr/sbin/parprouted $ETHERNET_IFACE $WIRELESS_IFACE
## Reloading DHCP Relay
echo "Start / Reload DHCP Relay..."
/bin/systemctl restart dhcp-helper
## A little sleep
sleep 2
## Refresh local ARP cache
echo "Refresh local ARP cache..."
/sbin/ip -s -s neigh flush all
}
function stop_service {
## Stop ARP forwarding
echo -e "\nKilling paraprouted..."
/usr/bin/killall -KILL parprouted 2> /dev/null
## Stop DHCP Relay
echo "Stoping DHCP Relay..."
/bin/systemctl stop dhcp-helper
## Remove assigned address
echo "Removing attached IP to ${ETHERNET_IFACE}..."
/sbin/ip addr del $(/sbin/ip addr show $WIRELESS_IFACE | perl -wne 'm|^\s+inet (.*)/| && print $1')/32 dev $ETHERNET_IFACE
## Stop ethernet interface
echo "Setting lan interface down..."
/sbin/ip link set $ETHERNET_IFACE down
## Disable system forwarding
echo "Disable IP forwarding..."
echo 0 > /proc/sys/net/ipv4/ip_forward
## Refresh local ARP cache
echo "Refresh local ARP cache..."
/sbin/ip -s -s neigh flush all
}
function service_status {
IP_FORWARDING_STATUS=$(cat /proc/sys/net/ipv4/ip_forward)
ARP_ROUTING_PROC=$(ps aux | grep -v grep | grep -i parprouted)
## Display network interfaces config
echo -e "\nNetwork interfaces:\n"
/sbin/ip -c a
## Display IP forwarding status
if [[ $IP_FORWARDING_STATUS == '0' ]]; then
echo -e "\nIP forwarding: disabled"
else
echo -e "\nIP forwarding: enabled"
fi
## Display ARP routing process
echo -e "\nARP Routing process:\n$ARP_ROUTING_PROC"
## Display ARP table
echo -e "\nARP table:\n"
/usr/sbin/arp -vn
## Display DHCP Helper status
echo -e "\nDHCP Relay status:\n"
/bin/systemctl status dhcp-helper
}
function generic_actions {
## Disable wireless power management
## Set the variable to true if your wireless speed is too low
if [[ $DISABLE_POWER_MGMT == true ]]; then
/sbin/iwconfig $WIRELESS_IFACE power off
fi
}
## Actions (Generic)
generic_actions
## Actions (Service)
case "$1" in
'start')
start_service
;;
'stop')
stop_service
;;
'restart')
stop_service
sleep 5
start_service
;;
'status')
service_status
;;
*)
echo -e "\nInvalid argument.\n"
;;
esac
## End Process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment