#!/bin/sh # IsHostAlive.sh # Mon Dec 8 22:41:56 PST 2008 # Chieh Cheng # http://www.CynosureX.com/ # GNU General Public License (GPL) Version 2, June 1991 scriptName=`basename "$0"` checkAllHosts () { while read host do result=`checkHost "${host}"` printf "${host} is " if [ ${result} -eq 1 ] then echo "alive" else echo "dead" fi done } checkHost () { host="$1" os=`uname -s` case "${os}" in "Darwin") output=`ping -c 1 -t 1 ${host} 2>&1` ;; "Linux") output=`ping -c 1 -W 1 ${host} 2>&1` ;; "SunOS") output=`ping "${host}" 1 1 2>&1` result=`echo "${output}" | grep -c " is alive"` if [ ${result} -eq 1 ] then output=" 0% packet loss" else output=" 100% packet loss" fi ;; *) output="Unsupported operating system." echo "${output}" ;; esac # Darwin shows 0.0% packet loss. # Linux shows 0% packet loss. result=`echo "${output}" | grep -c ' \(0.\)*0% packet loss'` echo ${result} } usage () { echo " Usage: ${scriptName} host" echo " or" echo " ${scriptName} -" echo echo " With the '-', you can pipe in a list of hosts." } if [ $# -eq 1 ] then host="$1" if [ "${host}" = "-" ] then checkAllHosts else checkHost "${host}" fi else usage fi