Script to Ping Servers

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

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.

#!/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.

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

for (( count=1; count <= $COUNT; count++ )) 
do 
ping -c 1  $HOST
done

to see if i can make that work instead.

If they didn't tell you the 'different way' they wanted, I sympathize a lot, it sucks to run a treadmill.

Since you have BASH:

REPLY=0

for ((N=0; N<PINGCOUNT; N++))
do
        ping -c 1 $host && ((REPLY++))
done

[ "$REPLY" = "$PINGCOUNT" ] || echo "$host is down"

Thank you for the quick response.

ok with that in mind, i created the below. however. i get a infinite looop.

what am i missing here?! :):confused::confused:

#!/bin/bash
#Written by: Emmanuel Iroanya Jr

#Date: 9/19/2013

HOST=$1
PINGCOUNT=$2
RESPONSE=0  

for ((COUNT=0; N<PINGCOUNT; COUNT++)) 
do         
	ping -c 1 $HOST && ((RESPONSE++)) 
done

if [ "$RESPONSE" = "$PINGCOUNT" ] 
then
echo "$HOST is up"
	return 0
fi

done

Just one thing you forgot to change:

for ((COUNT=0; N<PINGCOUNT; COUNT++)) 
1 Like

lol figures

THANKS!

I'say that if just a single one of the pings gets through, the host is up. Any number of pings less than COUNT points to network problems.