0

I have found the nice script below to ping a host to check if it is up or down. However I haven't found a way (and my very basic programming knowledge has failed me here) on how I can ping multiple servers. Ideally I'd go HOST1= XXX, HOST2=YYY etc...

Could someone point me in the right direction? I have about 10 hosts I'd like to see the up/down status for. Geektool script copied from macosxtips.co.uk

HOST=typehostnamehere
PING=`ping -q -c 1 $HOST`
if [[ $? -eq 0 ]] ; then
    TIME=`echo $PING |tail -1 | cut -d/ -f 5`
    echo ${TIME}ms away from $HOST
    exit 0
else
    echo $HOST could not be reached
    exit 2
fi
1

You want to read up on loops in bash scripts.

For example:

HOSTS="hosta
hostb
hostc"

for machine in $HOSTS
do
  PING=`ping -q -c 1 $machine`
  if [[ $? -eq 0 ]] ; then
    TIME=`echo $PING |tail -1 | cut -d/ -f 5`
    echo ${TIME}ms away from $machine
  else
    echo $machine could not be reached
  fi
done

I didn't test that, but it's close enough. Replace the HOSTS list with the list of hosts you want to report on.

1

To ping multiple hosts with the red/green feedback image you need to run multiple instances of the ping geeklet. Just open ping.glet multiple times, enter a different host name each time and line them up on your screen to get the effect seen in the screenshot at the site you linked.

You can rewrite the script to ping multiple sites but you'll only get one red/green feedback image.

  • Thanks, yep just figured that one out. Appreciate the feedback :) – pknz Aug 5 '14 at 0:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .