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 down.
Please assist in writing the script.

Show us what you've done so far... Where are your problems?

Hi,

first thing that comes to mind is to use a simple configuration file that contains the name of all the servers to be pinged, one per line.

Then you could write a script that reads each line and for each hostname read it pings, like i.e.:

while read line
do
   #   one ping to the current hostname
   ping -c 1 ${line} > /dev/null 2>&1
   #   get the exit code of ping command
   RETVAL=$?
   if [ ${RETVAL} -ne 0 ] ; then
      #   log error somewhere, send a mail to someone, etc.
   fi
done < /path/to/config_file

see ya
fra

1 Like

What is the problem exactly, ping takes a -c <count> modifier to exit after count packets so if you have a file with all the hostnames in it.

for host in $(cat network_hosts);do 
   if [ ! "$( ping -c 1 $host 2>/dev/null| grep '1 received' )" ] ; then 
      echo "$host is unresponsive"
   fi
done
1 Like

Hi

Below is the file containing the hostnames of all the servers

root@[/] #cat network_hosts
frssit02.standardchartered.co.in
frssit01.standardchartered.co.in
cbspssit.standardchartered.co.in
cbspsuat.standardchartered.co.in
cbsappfs1.standardchartered.co.in
cbsappfs2.standardchartered.co.in
cbdodbfs2.standardchartered.co.in

All the servers are live except cbdodbfs2.standardchartered.co.in as we have shutdown this lpar.

Please suggest me a script which will tell that "cbdodbfs2.standardchartered.co.in" is not alive and the rest are alive.

Hi,

how should the script report the downed servers? (email, log file, a message on the screen,...)

@newtoaixos:
What is not ok with the script frappa offered to you? What is missing? What did not work? Did you try it? Did you try anything?
Additionally just scroll down to the bottom of this thread and you see some similar threads where people try to ping hosts.

@ frappa : A message on the screen should be fine.
@ Zaxxon : I don't have any knowledge on scripts. I have added the content of frappa's script to my network_hosts file. The below is the current output from the file.

root#cat network_hosts
frssit02.standardchartered.co.in
frssit01.standardchartered.co.in
cbspssit.standardchartered.co.in
cbspsuat.standardchartered.co.in
cbsappfs1.standardchartered.co.in
cbsappfs2.standardchartered.co.in
cbdodbfs2.standardchartered.co.in
while read line
do
#   one ping to the current hostname
ping -c 1 ${line} > /dev/null 2>&1
#   get the exit code of ping command
RETVAL=$?
if [ ${RETVAL} -ne 0 ] ; then
#   log error somewhere, send a mail to someone, etc.
fi
done

When I give

#./network_hosts it comes up with the message as
 "0403-057 Syntax error at line 14 : `fi' is not expected."

Please assist

you have no instruction inside the if...fi group

frappa was fairly clear in saying that the hostname list was a seperate file which was sent to the new file thus saving the following as checker.sh

#!/bin/bash
while read line
do
   #   one ping to the current hostname
   ping -c 1 ${line} > /dev/null 2>&1
   #   get the exit code of ping command
   RETVAL=$?
   if [ ${RETVAL} -ne 0 ] ; then
      echo $line is not responding
   fi
done  < $1

and calling it like this . checker.sh hostlist would return a list of failing servers

1 Like

My bad :frowning:
Thanks Skrynesaver. It works fine now