Skip to main content

Simple shell script to ping remote hosts for connection availability.

#!/bin/sh

##
# Ping Options
#
# `-q`
#   Quiet output.
#
# `-c` <number>
#   Stop after sending/receiving N response.
#
# `-t` <seconds>
#   Timeout seconds.
##

remote_hosts="google.com jonlabelle.com"
ping_count=4

echo
echo 'Pinging Remote Hosts'
echo

for remote_host in $remote_hosts
do
    echo -n "- $remote_host... "
    response_count=$(ping -c $ping_count -t 3 $remote_host | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
    if [ $response_count -eq 0 ]; then
        echo "FAILED"
    else
        echo "OK"
    fi
done

echo
echo 'Finished.'
echo

# ping -q -c5 -t 3 google.com > /dev/null
# if [ $? -eq 0 ]; then
#     echo "Internet is available."
# else
#     echo "Internet is NOT available."
# fi