Operating System - HP-UX
1833682 Members
4831 Online
110062 Solutions
New Discussion

Using Ping to verify status

 
SOLVED
Go to solution
Jeff Paciolla_1
Frequent Advisor

Using Ping to verify status

Has anybody ever used a ping command in a script to verify the up/down status of an interface using the ip address?

Example:
In my script if interface 1 goes down my script captures the ip address & compares it to a static list of ip addresses. If it does not find a match it exits.

On the other hand if interface 2 goes down the same comparison is done, this time there is a match so the script sleeps for 3 minutes. After 3 minutes I want to ping the ip address of interface 2 to verify that it is still down before the rest of the script runs, which sends out a page and popup box.

Foe some reason I can't seem to get the syntax right in the ping statment as well as the results for verification.

Any and all help is appreciated.

Thanks Jeff
10 REPLIES 10
harry d brown jr
Honored Contributor

Re: Using Ping to verify status

You'll need to post a sniplet of your code, so that we can analize it (tear it apart - just kidding).

live free or die
harry
Live Free or Die
Jeff Paciolla_1
Frequent Advisor

Re: Using Ping to verify status

fair enough - Here is the portion of the code.


#Determining which interface is down in NNM

HOSTNAME=$1
SERINT=$2
DATE=$3
TIME=$4

#Capturing the right data from Database text file

SERIALIP=`awk -v HOSTNAME="${HOSTNAME}" -F";" '{if($1==HOSTNAME) {print $7}}' /opt/OV/contrib/NNM/ov
alarm/configinfo.txt`

#Verifying that the interface down is the serial interface

if [ "$SERINT" = "$SERIALIP" ] ; then
#Waiting three minutes for true outage
#sleep 180

Here is where I need help!!

#Verifying that interface is still down
#ping $SERINT
if $SERINT unreachable ; then
echo "$SERINT"
fi

else
echo "$DATE | $TIME | Hostname:$HOSTNAME | Interface:$SERINT\n" >> /home/root/scripts/log/ipche
ckdown.log
exit 0
fi

Thanks
Jeff
Darrell Allen
Honored Contributor

Re: Using Ping to verify status

Hey Jeff, how about a little more info such as: is the script running on the same box that has the interfaces you want to check? I don't grasp what you're trying to do yet.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Eugen Cocalea
Respected Contributor

Re: Using Ping to verify status

Hi,

'sed' the output of the ping command?

E.
To Live Is To Learn
Robin Wakefield
Honored Contributor
Solution

Re: Using Ping to verify status

Hi Jeff,

How about:

/etc/ping $SERINT 8 1 | grep 100% >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo $SERINT unreachable
fi

Rgds, Robin
Bill Hassell
Honored Contributor

Re: Using Ping to verify status

There are several considerations to this task. ping works with IP addresses so if you use a list of hostnames, you will run the risk of failing because DNS may not be working. I would recommend IP addresses for reliability.

In 10.20, ping has a nice option: -m so you can override the default wait time for a ping to return (ie, ping bambam -m 3) but unfortunately, -m was removed at 11.0 and later versions.

So an interesting design for your script would be to start a ping in the background, obtain the PID for that process using something like: PINGPID=$!, then sleep for one second, check the status of that process ID using: ps -p $PINGPID. If the process has disappeared, then it finished and you can break out of the loop.

After some number of loops (perhaps 5 or 10), and ping is still running, just issue: kill $PINGID and report that ping failed. Be sure to use -n to limit the number of test pings (such as -n 1) and perhaps adjust the packet size as appropriate.


Bill Hassell, sysadmin
Darrell Allen
Honored Contributor

Re: Using Ping to verify status

Hi Jeff,

You posted you script before I saw. Oops!

ans=`ping addr -n 1 | grep "100% packet loss"`
if [ X"$ans" != X ] ; then
unreachable


Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
someone_4
Honored Contributor

Re: Using Ping to verify status

hi .. here is what i use ..
You can modify to make it into a proccess. Or you can make it a cron job. Note that the file hostnames is a seperate file that has a list of the hosts names or IPs you are trying to test. like so

host1
host2
host3
192.xxx.xx.xx


If you are going to do it by host make sure that dns works or you have the hostsname in /etc/hosts

Here is the script

#!/bin/sh
LANG=C
HOSTNAME_FILE=hostnames
for host in $(cat $HOSTNAME_FILE)
do
ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host: OK"
else
echo "$host: FAIL"
fi
done


This one will send you an email

#!/bin/sh
LANG=C
HOSTNAME_FILE=hostnames
for host in $(cat $HOSTNAME_FILE)
do
ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host: OK"
else
echo "$host: FAIL" | mailx -s "hostdown" user@domain
fi
done


Uday_S_Ankolekar
Honored Contributor

Re: Using Ping to verify status

Hi,

I use like this

if [ -n "`ping $1 64 1 | grep icmp`" ]
then echo "ALIVE"
else echo "DEAD"
fi ;;

-USA..
Good Luck..
Sachin Patel
Honored Contributor

Re: Using Ping to verify status

Hi Jeff,
Look this thread from yesterday.
Lauri was trying to do may be same thing as you want

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x73bee7726eccd5118ff10090279cd0f9,00.html

Sachin
Is photography a hobby or another way to spend $