Script to Ping Servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Ping Servers
# 1  
Old 09-19-2013
Script to Ping Servers

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

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; 09-19-2013 at 04:03 PM.. Reason: Adding to explanation
# 2  
Old 09-19-2013
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:

Code:
REPLY=0

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

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

# 3  
Old 09-19-2013
Quote:
Originally Posted by Corona688
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:

Code:
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?! SmilieSmilieSmilie


Code:
#!/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

# 4  
Old 09-19-2013
Just one thing you forgot to change:
Code:
for ((COUNT=0; N<PINGCOUNT; COUNT++))

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-19-2013
Quote:
Originally Posted by Corona688
Just one thing you forgot to change:
Code:
for ((COUNT=0; N<PINGCOUNT; COUNT++))

lol figures


THANKS!
# 6  
Old 09-20-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to ping multiple servers

Hi I did the following script to ping multiple servers, but I keep on receiveing duplicate emails for one server that is down: #!/bin/bash date cat /var/tmp/servers.list | while read output do ping -c 1 "$output" > /dev/null if ; then echo "node $output is up" else ... (10 Replies)
Discussion started by: fretagi
10 Replies

2. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

3. Shell Programming and Scripting

Script with ping

I have a question is there any posibility for writing a script that you can see if a pc in your network is up of down , when you run this script for ex. ./test.sh 63 45 54 , which are the ip adresses of the computers , when you give the last digit of the ip adres as argument 63 , 45 and 54 are... (5 Replies)
Discussion started by: Roggy
5 Replies

4. Shell Programming and Scripting

Ping script to list of servers

Hi Friends, I have experience in redhat/ Ubuntu OS, but I am very new to solaries os. my servers OS is Oracle Solaris 10 8/11 s10x_u10wos_17b X86. I have a file contains 200 servers IPs one by one. now I want a script to chaeck which IPs are pinging, not pingning. I... (8 Replies)
Discussion started by: kumar85shiv
8 Replies

5. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

6. Shell Programming and Scripting

Using ping in script

Hi. I have a server with multiple network ports that need to be tested to a list of destinations. I'm trying to write a scripts to automate this but can't seem to get past an error and could use some help. I have two test files one contains the ip addresses of the onboard NICs and the other... (10 Replies)
Discussion started by: mikez104
10 Replies

7. Shell Programming and Scripting

Ping the hostname of many servers using a script

Hi We have some 300 servers in the Data center and some of them are running with AIX and some of them are running with Solaris. I need a script which can be run in one of the server and that script should ping the hostname of all the 300 servers. Also the script should notify if any server is... (9 Replies)
Discussion started by: newtoaixos
9 Replies

8. Shell Programming and Scripting

Animation Ping on Solaris Like Cisco Ping

Hi, I develop simple animation ping script on Solaris Platform. It is like Cisco ping. Examples and source code are below. bash-3.00$ gokcell 152.155.180.8 30 Sending 30 Ping Packets to 152.155.180.8 !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

9. UNIX for Advanced & Expert Users

script to ping servers

Hi , I would like to automate a script to ping all the unix servers perodically thru cronjob. Is there any script out there? If so Please give me. Thanks in advance. (2 Replies)
Discussion started by: sriny
2 Replies

10. AIX

Script to ping servers in a file

I would like to ping a list of servers in a text file. Can anyone help? (1 Reply)
Discussion started by: gbarkhor
1 Replies
Login or Register to Ask a Question