Operating System - HP-UX
1833707 Members
2135 Online
110063 Solutions
New Discussion

ping exitcode and timeout on HP-UX 11.00

 
SOLVED
Go to solution
CPHVF-U Guard
Advisor

ping exitcode and timeout on HP-UX 11.00

Hi.

When I ping a non-existing host, I have 2 problems.

1. I can't set a timeout (default ~10 sec.)
2. The exitcode is 0
10 REPLIES 10
Santosh Nair_1
Honored Contributor

Re: ping exitcode and timeout on HP-UX 11.00

Try using fping, much more robust, flexible and produces correct error results:

www.fping.com

-Santosh
Life is what's happening while you're busy making other plans
Frank Slootweg
Honored Contributor

Re: ping exitcode and timeout on HP-UX 11.00

For problem 2, you can use something like:

result=`ping $HOST -n 1 | grep '100% packet loss'`
if [ ! -z "$result" ]
then
echo ping failed
else
echo ping OK
fi

For problem 1, if you can determine the hardware station address (Station Address in lanscan(1M)) of the host, you can perhaps use the linkloop(1M) command (which has a timeout parameter) instead of ping. Note however that chosing a short timeout can lead to false negatives, i.e. host is up, but just it or the network are 'slow'.
CPHVF-U Guard
Advisor

Re: ping exitcode and timeout on HP-UX 11.00

Santosh: Unfortunately our policy denies us to install non-HP software ....

Frank: I use 'grep "packets from"'.
I can't use HW-adr. when running through a router ....

Thanks for the answers both af you.
(Why did HP remove -m, it worked in 10.20)
Santosh Nair_1
Honored Contributor

Re: ping exitcode and timeout on HP-UX 11.00

For the timeout, what you can do is kick off the ping in a background session and then sleep for $TIMEOUT and if the ping is still running, kill it . I'll try to put together some code when I get to work, (its 6:00 AM over here).

-Santosh
Life is what's happening while you're busy making other plans
CPHVF-U Guard
Advisor

Re: ping exitcode and timeout on HP-UX 11.00

OK, I could use something like

#!/bin/sh
if test $# -le 2
then
HOST=$1
TIMEOUT=2
test $# -eq 2 && TIMEOUT=$2

ping $HOST -n 1 >/dev/null 2>&1 &
sleep $TIMEOUT
PID=`ps -ef | grep "ping $HOST" | grep -v grep | awk '{ print $2 }'` >/dev/null 2>&1

if kill $PID >/dev/null 2>&1
then
echo down # Do stuff here
else
echo up # Do stuff here
fi
else
echo "Usage: $0 HOST [TIMEOUT]" >&2
fi

Problem:
Now for every host, the timeout is 2 sec.
I have to test 100 hosts, where 10 is down.

Normal ping of host is around 1/2 sec. if its up. 10 sec. if its down.

Whithout this script, the total time is:
90 * 1/2 + 10 * 10 = 145 sec.

Whith the script total time is:
100 * 2 = 200 sec.
Andreas Voss
Honored Contributor

Re: ping exitcode and timeout on HP-UX 11.00

Hi,

here my solution for 100 sec's:

for host in hostname ....
do
(ping $host -n 1) >/dev/null 2>&1 &
sleep 1
kill $! >/dev/null 2>&1
if [ $? = 0 ]
then
echo "Host $host is down"
else
echo "Host $host is up"
fi
done

It check if the ping process lives after one second (if the process is gone, kill returns a non zero status)

Regards
Santosh Nair_1
Honored Contributor
Solution

Re: ping exitcode and timeout on HP-UX 11.00

Hi,

Okay did some scripting...see if this helps any:

#!/bin/sh
if test $# -le 2
then
HOST=$1
TIMEOUT=5
test $# -eq 2 && TIMEOUT=$2

(ping $HOST -n 1 >/dev/null 2>&1)&
PID=$!

count=1
while [[ $count -lt $TIMEOUT ]]
do
ps -ef |grep "ping $HOST" |grep -v grep >/dev/null 2>&1
if [[ $? -ne 0 ]]
then
break
fi
let count=count+1
sleep 1
done

if kill $PID >/dev/null 2>&1
then
echo down # Do stuff here
else
echo up # Do stuff here
fi
else
echo "Usage: $0 HOST [TIMEOUT]" >&2
fi


Also, to speed things up, you could probably kick off several ping at one time and monitor them simultaneously...although I'm not sure what kind of a load pinging 100 machines would put on the machine.

-Santosh
Life is what's happening while you're busy making other plans
CPHVF-U Guard
Advisor

Re: ping exitcode and timeout on HP-UX 11.00

Thanks to all.

I will use some variant of the above solutions.
Bill Hassell
Honored Contributor

Re: ping exitcode and timeout on HP-UX 11.00

Just a note for shell scripters:

Using:

ps -ef | grep

is not a reliable way of locating a process at all. grep does not discriminate between ping and helping and pinger...they all return a value, which is not desired, especially if your script kills the wrong program.

Instead, use ps -C which allows you to specify the exact command name (ie, ping) and ps will find it for you. It does not use grep but instead goes through the process table for an exact match. In order to use ps -C (or ps -H and others), you'll need to temporarily set UNIX95 as a variable -- which cause XPG4 behavior. Compare these two commands:

ps -ef | grep sh

UNIX95= ps -fC sh

And sinc the script needs to kill a specific process, you probably should check for more than one copy of ping running.

Or an even better way is to bypass grep'ing for ping and just ask the shell what the PID was for the background process:

ping 12.34.56.78 &
PINGPID=$!

Now you know exactly whivh process to test or kill if needed.


Bill Hassell, sysadmin
CPHVF-U Guard
Advisor

Re: ping exitcode and timeout on HP-UX 11.00

This is a follow-up.
HP is working on a patch for HP-UX 11.00.

Exit status:
On 10.20 the exit-status is correct if patch: PHNE_19566/PACHRDME/English is installed.

This patch will (afaik) be "ported" to 11.00.


Timeout:
On 10.20 there is a -m option
ping -m 1 host -n 1

On 11.00 there is a (unsupported) -W option
ping -W 1 host -n 1

-m/-W is timeout in sec., and is only available with -n.