Test internet connectivity from bash script

Ensure you have curl installed and then you can use the following bash code in your scripts to test for internet connectivity.

 

#!/bin/bash

echo "Testing connection..."

curlOutput=$(curl -I http://google.com)

# Possible cases, there are more but here we cover:
# 407 which can mean bad proxy details,
# 200 which means everything is OK,
# 302 which is a redirection but for us that's OK too.

if [[ $curlOutput == *"407"* ]]
then
 echo "No internet connection, bad proxy username / password";
 return
fi

if [[ $curlOutput == *"200"* ]]
then
 echo "internet connection OK";
 return
fi

if [[ $curlOutput == *"302"* ]]
then
 echo "internet connection OK";
 return
fi

 

Enjoy đŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *