cancel
Showing results for 
Search instead for 
Did you mean: 

scripting and ping

SOLVED
Go to solution
Andi Rigauer
Regular Advisor

scripting and ping

Hi Script Gurus :)

OK I wanna do a ping but only once.
what I do is ping [IPADRESSS} 56 1
it works fine but how can I verify if it is not working? I have to send an interrupt when it fails.
How can I do this in a script

Thanks in advance

Andi
god, root where's the difference
11 REPLIES
Tom Geudens
Honored Contributor

Re: scripting and ping

Hi Andi,
There are a lot of posts about this. Just do a "script ping" search on the forums.

One good example :
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x7401a24d9abcd4118fef0090279cd0f9,00.html

Regards,
Tom
A life ? Cool ! Where can I download one of those from ?
Peter Kloetgen
Esteemed Contributor

Re: scripting and ping

Hi Andi,

you can simply check the return code of your ping- command:

ping ip_adress 56 1

if test $? -eq 0
then
:
else
exit 1
fi


this should do it for you.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
steven Burgess_2
Honored Contributor

Re: scripting and ping

Andi

host_name=hosts
for host in $(cat $host_name)
do
/etc/ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host" >> /success
else
echo "$host" >> /failure
fi
done

Your variable host_name contains the host names in the file hosts that you wish to ping

grep -q - waits for the return of 1 packet received then goes to the next host in the file

$? is the return value

0 = succesful ping
1 = unsuccesful ping

HTH

Steve
take your time and think things through
john korterman
Honored Contributor

Re: scripting and ping

Hi,

it is of course a personal matter which solution you prefer. The attached script checks on "100% package loss" in the ping attempt.
In case the attached script is used for pinging more than a single machine, it is important to delete the file to which the output of the ping command is redirected: you have to make sure that the content of this file actually comes from the machine you ping.


regards

John K.
it would be nice if you always got a second chance
Andi Rigauer
Regular Advisor

Re: scripting and ping

Thanks a lot for your answers,
but This I allready knew, the thing I wanted to know is how I can set a timeout for a ping.
let's say it should try it for 1 second and if the ping does not come back the return value should be 1.
god, root where's the difference
sven verhaegen
Respected Contributor
Solution

Re: scripting and ping

ping has no timeout parameters so if you would like tot do something like that you should use something like 'time' in a script to run together with the other scripting effectively sending a SIGKILL od SIGHUPP tot he process of ping to stop it of it exceeds the given time ....
...knowing one ignores a greath many things is the first step to wisdom...
Mark Greene_1
Honored Contributor

Re: scripting and ping

Unlike AIX and other unices, HP-UX's ping does not have a flag to specify wait time. According the man page, the default wait is 1 second.

HTH
mark
the future will be a lot like now, only later
A. Clay Stephenson
Acclaimed Contributor

Re: scripting and ping

Here is one method that addresses all your concerns:

This will timeout after 5 seconds; silently returns 0 if ok
ping.pl -t 5 remotehost
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "Ping ok"
fi

ping.pl -u will display full usage.


If it ain't broke, I can fix that.
rainer doelker
Valued Contributor

Re: scripting and ping

Hi Andi

I've tried it
ping IP -n1
will break after some time if the IP is not pingalbe. So I'd suggest to grep for the line package loss and check if it is 0% or 100%?

ping sensles_ip -n 1 |grep "packet loss" |awk '{print $7}' |cut -f1 -d"%"

will answer with 100

ping reachable_ip -n 1 |grep "packet loss" |awk '{print $7}' |cut -f1 -d"%"

will give you 0

You could then compare the result with an if statement.

What do you think.
HTH
Rainer
A. Daniel King_1
Super Advisor

Re: scripting and ping

I find that a single ping is quite unreliable. I use three or more packets with something along the lines of:

/usr/sbin/ping x.x.x.x -n 3 | /usr/bin/grep "100% packet loss" > /dev/null && echo Down! || echo Up!
Command-Line Junkie
T G Manikandan
Honored Contributor

Re: scripting and ping