alive

Hello,
sometimes it would be helpful if ping would just answer with “alive” (=> “Yes, I could ping it”) or “not alive” (=> “No answer while I was waiting”). A simple “0” (=> “alive”) or “1” (=> “not alive”) would be useful in a script.

This short shell script called “alive” is all we need:

#!/bin/sh
# alive

ping -c 1 -w 3 "$1" &>/dev/null
echo $?

# eof

Testing the script:

crissa@unseen:~$ alive 111.222.333.444
2
crissa@unseen:~$ alive 192.168.1.77
1
crissa@unseen:~$ alive 192.168.1.88
0

2 => error => no valid ip address
1 => not alive => valid ip address, no answer
0 => alive => valid ip address, successful ping

This shell script will use alive:

#!/bin/sh

if [ `alive $1` == "0" ]
then
  echo "$1 is alive"
else
  echo "$1 is not alive"
fi

# eof

Maybe the following would be easier to use:

#!/bin/sh

if ping -c 1 -w 3 "$1" &>/dev/null ;
then
  echo "$1 is alive"
else
  echo "$1 is not alive"
fi

# eof

Choose what ever suits your needs. ;-)
Bye, Tore

This entry was posted in General, Linux and tagged , , , . Bookmark the permalink.

Leave a Reply

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