Results 1 to 9 of 9
Well, i'm new to Linux and I'm trying to do the following.
I have 2 computers and i can ping them both but just as an experiment I want to ...
- 11-28-2007 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
Ping another computer if ping to 1st computer fails
Well, i'm new to Linux and I'm trying to do the following.
I have 2 computers and i can ping them both but just as an experiment I want to disconnect Computer A (which always gets pinged first). This will fail as it is disconnected and then I want it to go and ping Computer B. Eventually I want it to actually connect to the computer that is online.
Eg...something like (and yes I know the script is wrong I'm still learning so hopefully you can help)
#start of programme
ping A
if A not responding
then ping B
if B not responding
echo Communications down!
end if
end if
#end of programme
Also is it possible for me to do something like if ping A is successful then connect to A? and the same for B....
Thanks in advance.
- 11-28-2007 #2
Ping replies are very verbose and we can capture them and run grep on them.
so something on the line of...
Code:ping some_machine | grep "network host unreachable"
- 11-28-2007 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
I found that using grep caused my shell to display nothing. it didnt even show that it was attempting to ping the pc.....would you mind telling me what grep exactly does?
- 11-28-2007 #4
Grep is a regular expression matching utility. You can read the man page for more details. In this case it will display only lines that contain "network host unreachable".
- 11-28-2007 #5This seems strange to me. Who cares if you can ping to box A or box B? It sounds like what you presumably care about is whether you can connect to a certain (tcp?) port on those boxes. Unless I've read your post wrong, that is the true test.
Originally Posted by Linux_Newbie_2007
Meaning what? Connect how? What client/daemon? Maybe you should explain in clear detail what you're trying to do.
Originally Posted by Linux_Newbie_2007
- 11-28-2007 #6
I'd envision the program flow to look something like this:
- Can I connect to service port on box A? If so, fire up client and connect, and we're done. If not, continue...
- Can I connect to service port on box B? If so, fire up client and connect, and we're done. If not, continue...
- Fail noisily.
- 11-28-2007 #7Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
- 09-04-2008 #8Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
Try this:
test -z "`ping -c 1 -w 1 IPA | grep 100%`" || test -z "`ping -c 1 -w 1 IPB | grep 100%`" || echo "down"
More complicated version:
Code:#!/bin/bash IPA=.... IPB=.... if [ -n "`ping -c 1 -w 1 $IPA | grep 100%`" ]; then #A down, try B if [ -n "`ping -c 1 -w 1 $IPB | grep 100%`" ]; then echo "connection down!" else echo "connecting to B" #to do: connecting to B fi else echo "connecting to A" #to do: connecting to A fi
- 09-04-2008 #9Just Joined!
- Join Date
- Aug 2008
- Location
- Seattle, WA
- Posts
- 46
instead of using grep, you could also use return codes, since ping returns 0 if it gets 100% response, 1 if it drops some (or all) and 2 of it breaks alltogether.
$? is the shell variable representing the return code
so things would look sort of like:
sorry about the pseudo-code, but the important part is $?Code:ping -c 3 hostA if $? == 0 connect hostA else ping -c 3 hostB if $? == 0 connect hostB else echo "OMG YOU BROKE IT!"


Reply With Quote
