1834418 Members
1935 Online
110067 Solutions
New Discussion

ping script

 
SOLVED
Go to solution
Ragni Singh
Super Advisor

ping script

Hey all,

I would like to write a script that would ping multiple servers. Any help is greatly appreciated. POints will be assigned.
13 REPLIES 13
Jean-Luc Oudart
Honored Contributor

Re: ping script

#!/bin/sh

for server in serv1 serv2 serv3
do
/etc/ping $server -n 3 > $server.ping
done
grep -l "100% packet loss" *.ping | cut -d"." -f1

this will give you the list of server you cannot ping

Jean-Luc
fiat lux
Ragni Singh
Super Advisor

Re: ping script

I want to ping to 2 IP addresses, and display the output to the screen my ping result. Any help is greatly appreciated.
harry d brown jr
Honored Contributor

Re: ping script

echo " vpart1 \n vpart2 \n vpart3 \n vpart4" | xargs -i ping {} -n 1


So if you have your hosts in a file as a list, then

cat THATFILE | xargs -i ping {} -n 1


live free or die
harry
Live Free or Die
BFA6
Respected Contributor

Re: ping script

How about this

for server in server1 server2
do
/etc/ping $server -n 3
done

Regards,

Hilary
Darrell Allen
Honored Contributor

Re: ping script

Hi,

Just a suggestion in addition to the good answers already given...

I'd go with "-n 3" to ping each server 3 times instead of just once. The script I wrote to ping remote locations would often give false failures when only sending 1 packet. I don't know the last time I had a false failure since I changed to send 3 packets.

It only takes 2 more seconds per location to send 3 packets than to send 1 even if there is no response.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
harry d brown jr
Honored Contributor

Re: ping script

Darrell,

Very GOOD point!

live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: ping script

Warning: icmp needs root permissions

Here's an example in perl

l1:/tmp 103 # cat xx
#!/pro/bin/perl -w

use Net::Ping;

my $icmp = Net::Ping->new ("icmp", 1);
my $tcp = Net::Ping->new ("tcp", 1);
my $udp = Net::Ping->new ("udp", 1);

my @hosts = qw(
l1
pc09
127.0.0.1
);

my %inet;
for my $ip (@hosts) {
$icmp->ping ($ip) and $inet{$ip}{icmp} = 1;
$tcp->ping ($ip) and $inet{$ip}{tcp} = 1;
$udp->ping ($ip) and $inet{$ip}{udp} = 1;
}
$icmp->close;
$udp->close;
$tcp->close;

print "IP address ICMP TCP UDP\n",
"-------------- ----- ----- -----\n";
for my $ip (@hosts) {
exists $inet{$ip} or next;
my %p = %{$inet{$ip}};
printf "%-14s%6s %6s %6s\n", $ip,
$p{icmp} ? "alive" : "- ",
$p{tcp} ? "alive" : "- ",
$p{udp} ? "alive" : "- ";
}
l1:/tmp 104 # perl xx
IP address ICMP TCP UDP
-------------- ----- ----- -----
l1 alive alive alive
pc09 alive alive alive
127.0.0.1 alive alive alive
l1:/tmp 105 #
Enjoy, Have FUN! H.Merijn
Allan Pincus
Frequent Advisor

Re: ping script

Hi,

I read all these responses, but it seems that everyone has assumed that these server are all WORKING. If you try and automate this, and a server is down, ping will hang. I looked at the man pages, and don't see a timeout option. Maybe someone knows....

...continuing, you have another option:

Let's suppose your on a class C network 11.40.1.xx (same applies for A and B). You can ping the broadcast address (assume 255)(this might be a bad idea in your environment, but in mine, this isn't a problem).

For example,

/etc/ping 11.40.1.255 -n N > /tmp/myPingFile

Where "N" is larger than the largest number of hosts on your network. (If you have a large network you may not want to do this).

Any hosts that are down won't echo back. Then you can read myPingFile, grep out the IP address into nslookup to see what hosts are alive.

Then you can compare this long list to your short list with another grep or something.

Anyway, ping is not a very friendly tool if you don't get a directed response back.

I tend to think about what can go wrong, too!

Good luck!!

- Allan
A. Clay Stephenson
Acclaimed Contributor

Re: ping script

Well, if you are concerned about timeouts then the attached perl script, ping.pl addresses that.

ping.pl -t 5 remote_host
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Host ok"
else
echo "Host Bad"
fi

and a timeout of 5 seconds is set.

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

Re: ping script

Hi Allan,

Actually, down hosts are why you use the "-n" option. On my 11.0 servers, "ping down_host -n 1" will return (timeout) after 11 seconds. "-n 3" returns after 13 seconds.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Allan Pincus
Frequent Advisor

Re: ping script

Darrell -

Yeah, I see that. If you have a bunch of hosts, that are down, this would still stink...

- Allan
someone_4
Honored Contributor
Solution

Re: ping script

Hi,
Here you go here is the script:

#!/bin/sh
LANG=C
HOSTNAME_FILE=iphosts
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


and here is how it works ..
iphosts is a file that has the list of hostnames or ip address like so

host1
host1
192.168.10.xx
192.168.10.xx


now you run the script and it will check the host and go to the next one you will get the output to the screen. If you modify it a little you can even get the scrip to send you an email

~ Richard

Stefan_6
Frequent Advisor

Re: ping script

Just flew over your request: linux' fping seems to be a possible solution:
- shows if interface is up or down
- ping with config-file, which you can edit
etc etc

have a look at it ...

stg