For anyone having similar issues: I solved it with with a custom bash script and systemd service for now.
It checks for an existing connection on eth0
and if it cannot detect one a hotspot on wlan0
is created.
#!/bin/bash
# ethernet_check.sh
# This script checks if the Ethernet cable is plugged into eth0 and manages the WiFi hotspot accordingly.
while true; do
# Check if the Ethernet cable is connected to eth0
if ethtool eth0 | grep "Link detected: yes" > /dev/null; then
echo "Ethernet cable is plugged in. Disconnecting WiFi hotspot..."
nmcli device disconnect wlan0
else
# Check if the hotspot is already running to avoid trying to start it again
hotspot_status=$(nmcli con show --active | grep Hotspot)
if [ -z "$hotspot_status" ]; then
echo "No Ethernet cable detected and no active hotspot. Starting WiFi hotspot..."
nmcli device wifi hotspot ifname wlan0 ssid YourSSID password YourPassword
else
echo "Hotspot is already active."
fi
fi
# Wait for 10 seconds before checking again
sleep 10
done
Make it executable with
chmod +x /usr/local/bin/ethernet_check.sh
Create a service to start it with the Raspberry Pi.
[Unit]
Description=Ethernet Hotspot Check Service
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/local/bin/ethernet_check.sh
Restart=on-abort
[Install]
WantedBy=multi-user.target
Reload systemd:
sudo systemctl daemon-reload
and enable the service to start on boot:
sudo systemctl enable ethernet_check.service
Then start the service or reboot.