This bash function uses netcat
to test for internet connection (by pinging google.com) so it should work on most Linux computers. We use it on our Raspberry Pies because some times if the internet is disconnected, it won’t establish a new connection once the network is back up. To bypass the problem we trigger a reboot which re-initialises network connection.
#!/bin/bash
# Description: Reboot machine if internet is down
function check_internet_connection() {
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Online"
else
echo "Offline"
sudo reboot
fi
}
check_internet_connection