Created
September 15, 2015 14:45
-
-
Save jeffskinnerbox/a59cd78b8a821c4787a0 to your computer and use it in GitHub Desktop.
This utility checks to see if a host is up or down
This file contains 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 | |
# This utility checks to see if a host is up or down | |
#set -x # set for debugging | |
# set test flag | |
TEST=0 | |
# Parse command line options | |
USAGE="Usage: `basename $0` [-t] [-h] host-name | IP-address" | |
while getopts ht OPT; do | |
case "$OPT" in | |
h) | |
echo $USAGE | |
exit 2 | |
;; | |
t) | |
TEST=1 | |
;; | |
\?) | |
# getopts issues an error message | |
echo $USAGE >&2 | |
exit 2 | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) # This tells getopts to move on to the next argument | |
TARGET=$1 | |
# ping the host to see if it is up | |
COUNT=$( ping -c 1 $TARGET | grep icmp* | wc -l ) | |
if [ $TEST -eq 0 ] | |
then | |
if [ $COUNT -eq 0 ] | |
then | |
echo "Host is not Alive! Try again later..." | |
else | |
echo "Yes! Host is Alive!" | |
fi | |
else | |
if [ $COUNT -eq 0 ] | |
then | |
exit 1 | |
else | |
exit 0 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment