Created
February 23, 2024 09:58
-
-
Save ph20/74c03cc6bfb3dfc4ae30570dddad48f2 to your computer and use it in GitHub Desktop.
Automatically Managing Network Routes on Raspberry Pi for Reliable Internet Connectivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# IP for Google's DNS server used for testing connectivity | |
TEST_IP=8.8.8.8 | |
# Ping TEST_IP using wlan0. If successful, ensure routing through wlan0 | |
if ping -I wlan0 -c 4 $TEST_IP > /dev/null; then | |
echo "Internet connectivity is available through wlan0. Adjusting routes..." | |
# Get the default gateway IP for wlan0 | |
GATEWAY_IP=$(ip route show default via | grep wlan0 | awk '{print $3}') | |
# Delete the default route for eth0 | |
sudo ip route del default dev eth0 | |
# Add a new default route via wlan0 if not already set | |
if ! ip route show default via $GATEWAY_IP dev wlan0 | grep -q 'default via'; then | |
sudo ip route add default via $GATEWAY_IP dev wlan0 | |
fi | |
else | |
echo "No Internet connectivity available through wlan0." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment