Visit Our UNIX and Linux User Community

Need a Simple ping script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a Simple ping script
# 1  
Old 09-15-2009
Java Need a Simple ping script


I am a learner in shell scripting. Can someone help me in getting a script to run the ping command in the background through a script to get the status of my servers and email me if ping failed with list of servers in one email.

# 2  
Old 09-15-2009
do you have something in your mind?

man ping

ping switches varies with OS. what flavor you have?

try something;
while read HOST
 ping $HOST
 if [ "$?" -ne "0" ]; then
  echo $HOST >> failed_host.$$
done < hostfile
if [ -s "failed_host.$$"];then
 cat failed_host.$$ | mailx -s failed server list

note: this is just an idea. not tested.
# 3  
Old 09-15-2009
Thanks for the response.

I have couple of hosts in the same network running on Solaris and Linux. Ping command varies from solaris and linux. We need a script to grep for uname first,then if it returns solaris i am using " `ping -s $myHost $SIZE $COUNT | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'`" . If uname returns linux then " (ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')". My question is if i run the ping for single host it takes min 10secs for the 4 packets to transmit. I need help in writing the script which executes the ping at background on all hosts and write to a file. Then parse the for "recieved -eq 0" then shoot an email with the ping failed hosts.

myHost = /home/user/listofhosts -- file stored with list of hosts(linux&solaris)
# 4  
Old 09-15-2009
ping -c 3 $SERVER

so it will stop after 3 pings
# 5  
Old 09-16-2009
This simple script should do the trick:
rm /tmp/pingfail.* 2>/dev/null
for host in $(cat $HOSTLIST); do
  ( ping -c 4 $host >/dev/null 2>&1 || touch /tmp/pingfail.$host)&
if ls /tmp/pingfail.*>/dev/null 2>&1; then
  for failhost in /tmp/pingfail.*; do
    echo ${failhost#*.}
  done | mailx -s "failed server list" 2>/dev/null
  rm /tmp/pingfail.* 2>/dev/null

Instead of ksh you can use bash too, just replace #!/bin/ksh with #!/bin/bash

Last edited by Scrutinizer; 09-16-2009 at 03:02 AM..
This User Gave Thanks to Scrutinizer For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #219
Difficulty: Easy
When a data packet comes in to a port on a router, the router reads the network address information in the packet footer to determine the ultimate destination.
True or False?

10 More Discussions You Might Find Interesting

1. 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="" 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

2. 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. ./ 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

3. Shell Programming and Scripting

ping script

hello fellows, I need help with a script, I'm using this one HOSTS=" " SUBJECT="Attention - Gateway San Martin DOWN " COUNT=1 EMAILID="" for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2... (2 Replies)
Discussion started by: lucas.fradusco
2 Replies

4. 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

5. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 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

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 30 Sending 30 Ping Packets to !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

8. Shell Programming and Scripting

Help With Ping Script

Hello all...I'm new to Unix and learning. What I'm trying to create is a script that will ping a known range of IP addresses, say to For each address that no reply is received, that address will be written to a log file that will be emailed to an administrator. My wife... (1 Reply)
Discussion started by: spmitchell
1 Replies

9. UNIX for Dummies Questions & Answers

Simple Ping script

Hi there i am busy compiling a script to ping a remote server, when the server is alive i get it to send me an sms, however i cant seem to make the ping stop, can somebody please help? please? while (9) set ans="`ping $IPaddress 1`" if ( "$ans" == "IPaddress is alive" ) continue ... (5 Replies)
Discussion started by: brian112
5 Replies

10. Shell Programming and Scripting

Ping Script

hello, I am looking to make a script that will ping a remote ip address. Upon completion of the ping I want the program to either ping again if the # of packets transmitted is equal to the number of packets received or exit if the two values are unequal and information was lost. I am not sure if I... (3 Replies)
Discussion started by: mcrosby
3 Replies

Featured Tech Videos