View Single Post
  #2 (permalink)  
Old April 28th, 2005, 09:41 AM
ciderpunx ciderpunx is offline
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

OK, this is a perl script that'll do it for you on a linux box.

You can use this in 2 ways (both examples output their data to a file called log.txt, only successful pings are output):

1. With a data file, 1 IP/Hostname per line.
  $ ./multiping.pl <filename> > log.txt

2. From the commandline, give it 1 IP/Hostname per line and end the list with ctrl-d, exit, or quit.
  $ ./multiping.pl > log.txt
  127.0.0.1
  1.0.0.127
  google.com
  quit

Example output (from eg.2):
  $ cat log.txt
  127.0.0.1 : 1 packets transmitted, 1 received, 0% packet loss, time 0ms
  google.com : 1 packets transmitted, 1 received, 0% packet loss, time 0ms

Code:
#!/usr/bin/perl
#
# multiping.pl: ping multiple hosts from a linux box
#
# Copyright (C)2005 Charlie Harvey
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# use_less_, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# Also available on line: http://www.gnu.org/copyleft/gpl.html

use strict;
my @ping_array=();

# read input from stdin.
while(<>) {
        last if (/exit|quit/i);
        chomp;
        # actually do the ping
        my $ping_result = `ping -q -c1 $_ | tail -n2 |head -n1`;
        chomp $ping_result;
        push @ping_array, "$_ :\t$ping_result";
}

foreach (@ping_array) {
        # only print out this result if there was 0% packet loss.
        next unless ($_=~/ 0% packet loss/i);
        print "$_\n";
}

######END

HTH
Charlie



--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
Reply With Quote