issue a ping on a remote box

Hi there

I am running a script on a central box (boxA) that will send a remote request to boxB to perform a ping test to an ip

note: I am not pinging boxB from boxA but sending a request over ssh to get boxB to perform a ping test !

The thing is, I want the script back at boxA to know whether the remote ping was successful or not ...is this possible, i tried using this 'if' test condition hoping that a true/false would come back, am i doing something wrong?

[root@boxA] # vi test  

#!/bin/ksh -x

if [ `ssh boxB 'ping 172.1.1.1'` ]; then
echo "can ping from boxA"
else
echo "cant ping at all"
fi

NOTE: this ip is in fact definitely pingable from boxB but I get the following return from my script

[root@boxA] # ./test
+ FCEDIT=vi
+ export FCEDIT
+ set -o vi
+ + uname -n
PS1=boxA$ 
+ EDITOR=vi
+ export EDITOR
+ ssh boxB ping 172.1.1.1
+ [ 172.1.1.1 is alive ]
./test[5]: is: unknown test operator
+ echo cant ping at all
cant ping at all

As you can see from the bit highlighted in red above, the remote ping seems to be working, but how do I get my local script on boxA to be made aware of this

Any help on this would be greatly appreciated

if [ "`ssh boxB 'ping 172.1.1.1'`" ]; then....

The problem is that when using ` xxx ` you will substitue that with the return of the command...

so when this is executed your code will look like this:

if [ 172.1.1.1 is alive ] then..

you need to use " " around that since if will see this as multiple args and doesnt know what do with it.

Hope this helps

ah, thankyou very much, however (something I should have probably mentioned at the start ) I will actually be passing an argument to script, ie ill be running

./test 172.1.1.1

so therefore the code will have to be

f [ "`ssh boxB 'ping $1'`" ]; then ....

by me doing this, the solution you provided doesnt work, although it works perfectly with the original example i gave ...apologies for not stating this at the beginning

Ive tried shifting the three different quote characters around to no avail ....can you see any way I can get this to work using a $1 rather that putting the actual IP in the command ?

Why not try assigning your check result to a variable like:

check=`ssh boxB "ping -s 1 -w 1 -c 1 $1" > /dev/null; echo $?`
if [ "$check" -eq "0" ]; then 
echo "ping successful!"
else
echo "can't ping!"
fi

That's just to simplify things.. at least for me.

thanks, i found another way too

from boxA

#!/bin/ksh

ssh boxB "ping $1 2 > /dev/null 2>&1"

if [ $? -eq 0 ]; then
echo "can ping"
else
echo "cant ping at all"
fi

thankyou all for your responses