1827251 Members
2825 Online
109716 Solutions
New Discussion

Re: ping script

 
SOLVED
Go to solution
Hunki
Super Advisor

ping script


For - Write a script that will ping a list of hosts and return how many are 'alive'.

I am trying out the following :

cat /etc/hosts|awk '{print $2}'|sort -u|grep -v ^$|xargs ping

it gives me the following error :

ping: bad timeout:hostname

when I pass the above command to a variable I get :

host1 host2 host3 host4

with no carriage returns . How do I overcome this.

Thanks.
12 REPLIES 12
Hunki
Super Advisor

Re: ping script


the result :

host1 host2 host3 is when I do echo $VAR
Pete Randall
Outstanding Contributor

Re: ping script

See Clay's answer to a remarkable similar question in this thread:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1149721


Pete

Pete
Juan M Leon
Trusted Contributor
Solution

Re: ping script

Hunki,
I will sugest to use the following

cat /etc/hosts | awk '{print $2}' | xargs -t -i ping {} -n 1
OR
for server in `cat /etc/hosts | awk '{print $2}'`
do
echo "$server : \c"
ping $server -n1 | awk '/transmitted/ {print $0}'
done


Hope it helps
Sandman!
Honored Contributor

Re: ping script

You need to specify how many packets the ping(1M) program should send out before moving onto the next hostname in the /etc/hosts file or until end of file:

# cat /etc/hosts | egrep -v '^#|^$' | xargs -n1 -i ping {} -n 2
A. Clay Stephenson
Acclaimed Contributor

Re: ping script

Use the attached Perl script and the rest is easy. You can select ICMP, UDP, or TCP as well as the number of tries and the timeout for each. All you have to do is test the result; 0 --> Good.

#!/usr/bin/sh

HOSTS="huey louie dewey"
for HOST in ${HOSTS}
do
ping.pl -t 5 -n 3 ${HOST}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "${HOST} is alive."
else
echo "${HOST} failed; status ${STAT}." >&2
fi
done
--------------------------------------

Invoke as ping.pl -u for full usage.
If it ain't broke, I can fix that.
Geoff Wild
Honored Contributor

Re: ping script

I put that in your other thread:

for i in `cat hosts`
do
ping $i -n 1 | grep "0%" >/dev/null
t=`echo $?`
ttl=`expr $ttl + $t`
done
echo "there are $ttl hosts alive..."


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Hunki
Super Advisor

Re: ping script


Thanks All for replying , I am looking at Sandman's solution ,

what does -n1 and -i signify. Please throw some light on this please.

Thanks,
James R. Ferguson
Acclaimed Contributor

Re: ping script

Hi:

> what does -n1 and -i signify

I suggest that you consult the manpages. You will learn more by researching questions like this yourself.

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: ping script

>> what does -n1 and -i signify. Please throw some light on this please.

More than anything, it signifies that you are too lazy/unmotivated/uninformed/... to do a "man xargs". Unless you learn to use the man pages well, you will never have a very successful career in UNIX.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: ping script

"-n1" and "-i" are arguments to xargs. This is different from the arguments to ping. The "-n1" argument to xargs means that the hostnames are passed one at a time to the ping command instead of being bundled together. And the "-i" argument [in this case] is a placeholder for the hostname and corresponds to the curly braces. It's a way of specifying where on the command line will the pipelined hostname argument fall. This is so that the arguments to ping(1M) i.e. hostname and "-n 2" [which is the number of packets that should be sent out before moving onto the next host or until the end of /etc/hosts is reached] are placed correctly on the command line.

~hope it helps
Juan M Leon
Trusted Contributor

Re: ping script

I always check with the man to find answer to command line and arguments.

LOL

-i is insert the previous stdout
-n uses number of arguments to pass from the previous stdout

good luck
Dennis Handly
Acclaimed Contributor

Re: ping script

You can of course replace every instance of evil/useless cat in the examples here:
awk '{print $2}' /etc/hosts |sort -u|grep -v ^$|xargs ping
grep -v -e '^#' -e '^$' /etc/hosts | xargs -n1 -i ping {} -n 2
$ for i in $(< /etc/hosts ); do