Use a more accurate way to check internet connection

when using wget, if the DNS resolver service take a long time to answer (minutes for example), the script take the same time to give a answer
This commit is contained in:
Eduard Tolosa 2018-11-28 09:16:20 -05:00
parent 5aa7a72223
commit 34fed3676f

View file

@ -6,15 +6,13 @@
# Autor: Eduard Toloza # Autor: Eduard Toloza
# Define variables # Define variables
ofile=/tmp/google.txt services='Put here the service names that you want to restart if internet connection is not available, the services must be sepparated by spaces. Example: systemd-resolved systemd-networkd openvpn'
services='write here the name of the services separated by spaces, example: systemd-networkd systemd-resolved wicd network-manager'
#Check if wget is installed #Check if ping command is available
if command -v wget > /dev/null; then if command -v ping > /dev/null; then
# Check internet connection and if not OK then restart all networking services # Check internet connection and if not OK then restart all networking services
echo "Testing your connection..." echo "Testing your connection..."
wget -q --tries=1 --timeout=1 https://google.com -O $ofile &> /dev/null if ! ping -q -c 1 -W 1 1.1.1.1 > /dev/null; then
if [ ! -s $ofile ]; then
echo "Your connection is not working, restarting your network services: $services" echo "Your connection is not working, restarting your network services: $services"
sudo systemctl restart $services sudo systemctl restart $services
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
@ -22,11 +20,9 @@ if command -v wget > /dev/null; then
else else
echo "An error has occurred, make sure that service names are correct." echo "An error has occurred, make sure that service names are correct."
fi fi
rm $ofile
else else
echo "Your connection is OK" echo "Your connection is OK"
rm $ofile
fi fi
else else
echo "wget is not installed, aborting." echo "ping command is not available, aborting."
fi fi