Operating System - HP-UX
1823251 Members
3372 Online
109648 Solutions
New Discussion юеВ

shell script - ping list of hostname

 
SOLVED
Go to solution
Nik
Advisor

shell script - ping list of hostname

-- HPUX 10.20 --
I have a list of hostnames in a text file (1 host to each line) and I would like some help on writing a script that could ping each of these hostnames and report back which hosts did not respond?
Sorry, but so far I have no idea how to start this... So I thought I'd post this as I browse through Essential SysAdm for any tips... thanks people!
11 REPLIES 11
Tony Constantine_1
Regular Advisor

Re: shell script - ping list of hostname

could try

for HOSTNAME in hosta hostb hostc hostd
do
ping $HOSTNAME -n 1
done
Rainer_1
Honored Contributor
Solution

Re: shell script - ping list of hostname

Here my little script
#!/bin/sh

LANG=C
HOSTNAME_FILE=..... # insert here your file
for host in $(cat $HOSTNAME_FILE)
do
ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host: OK"
else
echo "$host: FAIL"
fi
done
Tony Constantine_1
Regular Advisor

Re: shell script - ping list of hostname

Sorry if there in a text file it will be

for HOSTNAME in `more /tmp/hostname.txt`
do
ping $HOSTNAME -n 1
done
Devbinder Singh Marway
Valued Contributor

Re: shell script - ping list of hostname

you can do something like this :-

for i in ` cat (your file with hosts) `
do
ping $i -n 1 | grep -q "100%"
if [[ $? = 0 ]]
then
print " network $i not reachable"
fi
done

HTH
Seek and you shall find
Patrick Wallek
Honored Contributor

Re: shell script - ping list of hostname

What I have done in the past is this:

script

ping host1 64 5
ping host2 64 5
ping host3 64 5


I then run the script and redirect stderr and stdout to a file and then go look at the file to determine what responded and what didn't.

to run the script:

./script > script.out 2>&1

Nik
Advisor

Re: shell script - ping list of hostname

wow! that's fantastic!
thanks to everyone...
Patrick Wallek
Honored Contributor

Re: shell script - ping list of hostname

Points are much appreciated as well.
James R. Ferguson
Acclaimed Contributor

Re: shell script - ping list of hostname

Hi:

#!/usr/bin/sh
while read HOST
do
ping $HOST -n 3
done < /tmp/ping.lst

...JRF...
Rob Smith
Respected Contributor

Re: shell script - ping list of hostname

Try this:


#!/usr/bin/sh
##
###
####
#####
for x in `cat /etc/hosts | awk ' $1 !~ /^#/ { print $1 } '`
do
ping $x -n 5

if [ $? -ne 0 ]
then
grep $x /etc/hosts
fi
done
Learn the rules so you can break them properly.
Alan Riggs
Honored Contributor

Re: shell script - ping list of hostname

One note of caution: at 10.20, at least, ping returns 0 whether the packet has been lost or not. Testing for $? is not a valid way to determine whether connectivity exists. Here is a solution I have used in the past:

PING=`ping $SERVER -n 1|grep %| awk '{print $7}'`
if [ ${PING%%} = 0 ]
then
echo ping test succeeded
else
echo ping test failed
fi
RuudPeters
Occasional Advisor

Re: shell script - ping list of hostname

Hi NIK,

You might want to have a look at the perl script DOWNTIME. Visit WWW.FRESHMEAT.NET for the exact URL. This script does exactly that what you need.

Good luck !