#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0

    Script to Ping Servers


    Hey, It's me again! Still trying to learn to become a better scripter on the job

    New challenge for assistance, if anyone cares to help, and its two parted! First part, I wanted to create a script at work that would ping a server that was supplied in an argument, then a count (amount of times) it should be pinged.

    I came up with a solution that I thought worked.

    Code:
    #!/bin/bash
    #Written by: Manny
    #Date: 9/19/2013
    
    
    HOST=$1
    COUNT=$2
    
    for hosts in $HOST
    do
      ping=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
      if [ $count -eq #IwantToCheckAgainstHalfofCOUNT ];
    then
            echo "Host : $hosts is down"
            return 1
      fi
        else return 0
    done
    I was pretty much told, "No, I want it done a different way" (There is a method to their madness I suppose)

    Any-ways, I have some pseudo code of how I interpret how they want it, but cant figure out how to get both Counters to work.

    Code:
    while COUNT <= number_of_times_i_ask_it_to_ping
    if ping -c1 host_i_ask_it_to_ping
    PINGCOUNT++
    fi
    COUNT++
    done
    So Any Help Making that into something i can use would be greatly appreciated it.

    also. if you see my first solution. I have a threshold of 50% of packets lost that i wanted to test that if statement against, but couldn’t think of a way to do that either. so for my own PERSONAL reasons, if anyone can think of a cool way to go about doing that, would be greatly appreciated as well.


    Quick Edit: in my pseudo code, i use a while loop, but it doesn't have to be. I'm sitting here playing with for loops like
    Code:
    for (( count=1; count <= $COUNT; count++ ))
    do
    ping -c 1 $HOST
    done
    to see if i can make that work instead.
    Last edited by gkelly1117; September 19th, 2013 at 02:03 PM. Reason: Adding to Explanation
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,464
    Rep Power
    1751
    I did something a little similar a long while back.
    What I did was to have a script that would ping a specific hostname (just one ping) and checked the response. If the packet was lost it would 'queue up' another test in a minute or two (using at now +? minute) to re-test and then, and only then, declare the box to be down.
    In essence it comes down to what you wish to do with this script. From the first version it looks like you might be able to supply a list of hosts to check (presumably in quotes as you have it as $1, but then use 'for hosts in'. So will this be for one, many or either? Is it to decide if a host is up or down (depending on ping response)?

    What 'they' have asked for is to have the host ping-ed a number of times (a number which can be specified?) and if some threshold of fails happens then that host is deemed to be down? If so, you do not need to ping -c 1 X times, that is just the same, really, as ping -c X but just adds the extra complexity of another loop.

    Testing the return you'll need to drop the % symbol off the packet loss percentage before you test it (and don't use equality test, use greater than or less than as it is unlikely to be an exact match)
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Location
    Baltimore, MD
    Posts
    29
    Rep Power
    0
    Originally Posted by SimonJM
    I did something a little similar a long while back.
    What I did was to have a script that would ping a specific hostname (just one ping) and checked the response. If the packet was lost it would 'queue up' another test in a minute or two (using at now +? minute) to re-test and then, and only then, declare the box to be down.
    In essence it comes down to what you wish to do with this script. From the first version it looks like you might be able to supply a list of hosts to check (presumably in quotes as you have it as $1, but then use 'for hosts in'. So will this be for one, many or either? Is it to decide if a host is up or down (depending on ping response)?

    What 'they' have asked for is to have the host ping-ed a number of times (a number which can be specified?) and if some threshold of fails happens then that host is deemed to be down? If so, you do not need to ping -c 1 X times, that is just the same, really, as ping -c X but just adds the extra complexity of another loop.

    Testing the return you'll need to drop the % symbol off the packet loss percentage before you test it (and don't use equality test, use greater than or less than as it is unlikely to be an exact match)
    I never got to thank you for your input, but thanks it definitely didnt go un noticed
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,464
    Rep Power
    1751
    No worries, hope it helped in some way, even if it was a "oh god, that's really NOT how I'm doing it ..."
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo