Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Linux Enthusiast apoorv_khurasia's Avatar
    Join Date
    Feb 2005
    Location
    Laurasia
    Posts
    624
    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"
    "There is no sixth rule"
    --Rob Pike
    Registered Linux User: 400426 home page

  3. #3
    Just 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?

  4. #4
    Linux Enthusiast apoorv_khurasia's Avatar
    Join Date
    Feb 2005
    Location
    Laurasia
    Posts
    624
    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".
    "There is no sixth rule"
    --Rob Pike
    Registered Linux User: 400426 home page

  5. #5
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Quote Originally Posted by Linux_Newbie_2007
    #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
    This 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.

    Quote Originally Posted by Linux_Newbie_2007
    Also is it possible for me to do something like if ping A is successful then connect to A? and the same for B....
    Meaning what? Connect how? What client/daemon? Maybe you should explain in clear detail what you're trying to do.

  6. #6
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    I'd envision the program flow to look something like this:
    1. Can I connect to service port on box A? If so, fire up client and connect, and we're done. If not, continue...
    2. Can I connect to service port on box B? If so, fire up client and connect, and we're done. If not, continue...
    3. Fail noisily.

  7. #7
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    Quote Originally Posted by Linux_Newbie_2007 View Post
    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?
    Make sure you specify a count for ping or it will run indefinitely thus preventing grep from working.
    Code:
     ping -c 1 host

  8. #8
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Quote Originally Posted by Linux_Newbie_2007 View Post
    #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.
    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

  9. #9
    Just 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:
    Code:
    ping -c 3 hostA
    if $? == 0
          connect hostA
    else
          ping -c 3 hostB
          if $? == 0
               connect hostB
          else
               echo "OMG YOU BROKE IT!"
    sorry about the pseudo-code, but the important part is $?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...