Simple Perl Multiple Host Ping Sweep Script

multihostPing

I have simplified my original multiple host ping sweep script. All you need for this script to work is a hosts.txt file with a list of host names or IP addresses and of course the script below.

Enjoy 🙂

Linux, Perl Scripts

9 comments to Simple Perl Multiple Host Ping Sweep Script

  • thepuregeek

    Reblogged this on .

    Reply
  • francisco

    hey. just found this nice scipt.
    I have the same issue. Just a couple of questions:

    Do i read it correctly that you are pinging icpm? i suspect that i can change this just to tcp is this correct?
    Is it possible to give the ping a flag? like -s?
    And last, how do i wirte the output to a cvf file, seperated by a ; ?

    Sorry for the questions, but i ame no hero at perl :P.

    Reply
    • thepuregeek

      Greetings,

      To answer your first question, yes I’m pinging with icmp protocol. Also, you are correct that you can change it to tcp. Actually, as this line below shows you can use any of these protocols with the ping module. (Protocol for ping must be “icmp”, “udp”, “tcp”, “syn”, “stream”, or “external”).

      Lastly, Perl also has another module that we can use to convert output to csv format. I’ve never used this module and would need some time to get familiar with it, however, you can research it also. It is called Text::CSV; Hope this helps.

      Reply
  • francisco

    thank you for your quick reply. I changed your script a bit. I need to check this if it works on the machine where it is supoused to run.
    The changes i made where simple.
    1.- change icmp to tcp.
    2.- add -s to the ping command (we need to know the laytency of the machines, and do a output to cvs).
    I am reseaching at the moment Text::CSV.
    Unlucky for my, i am no scripting hero, so, need to spend some time here. I suspect that i have already found some answers using our friend google :D.

    I will upload the script as soon i have it done.

    Reply
  • francisco

    i have writen this one, based on your script.

    Can you please provide any comment?
    The idea of this script is, that it pings a list of ips, (ip.txt).
    It writes the output to a *.csv
    The output is done per line, so it can be easly edited per line.

    #!/usr/bin/perl -w
    use strict;
    use Net::Ping;
    use Text::CSV;

    my $hostList = $ARGV[0];
    if(!defined $hostList){
    print “Usage: $0 n”;
    exit;
    open(LIST, $hostList) or die “Can’t open ip.txt file to load into array: $!”;
    }
    our @hosts = ;
    close(LIST);
    my $p=Net::Ping -s->new(“tcp”, 2);
    my @data;
    foreach my $host(@hosts){
    chomp($host);
    print “Pinging $hostn”;
    unless($p->ping ($host)){
    print “$host has failed: “,scalar(localtime),” n”;
    push(@data,“$host has failed: “,scalar(localtime),” n”)
    }else{
    print “$host was a success: “,scalar(localtime),” n”;
    my $csv=Text::CSV->new(); # push MYFILE “field1;field2;field3n”;
    }
    }

    #Save in output file
    my $output = “pinging “.scalar(localtime).”.csv”;
    open(LIST, $output) or die “Can’t open output file to write the result: $!”;
    foreach(foreach my $line(@data)
    {

    }
    exit;

    thank yu.

    ps, anyone can use this script, otherwise i would not put it online.

    Reply
  • francisco

    Hello there.

    We have wronten the following script:

    #!/usr/bin/perl -w
    use strict;
    use Net::Ping;
    use Text::CSV;

    my $hostList = $ARGV[0];
    if(!defined $hostList){
    print “Usage: $0 n”;
    exit;
    open(LIST, $hostList) or die “Can’t open hosts.txt file to load into array: $!”;
    }
    our @hosts = ;
    close(LIST);
    my $p=Net::Ping -s->new(“tcp”, 2);
    my @data;
    foreach my $host(@hosts){
    chomp($host);
    print “Pinging $hostn”;
    unless($p->ping ($host)){
    print “$host has failed: “,scalar(localtime),” n”;
    push(@data,“$host has failed: “,scalar(localtime),” n”)
    }else{
    print “$host was a success: “,scalar(localtime),” n”;
    my $csv=Text::CSV->new(); # push MYFILE “field1;field2;field3n”;
    }
    }

    #Save in output file
    my $output = “pinging “.scalar(localtime).”.csv”;
    open(LIST, $output) or die “Can’t open output file to write the result: $!”;
    foreach(foreach my $line(@data)
    {

    }
    exit;

    This script is suposed to do the following:

    1. read all the ip hosts from hosts.txt.
    Do a ping -s on the hosts (we need to know the latancy of the connections). We only use ip addresses to ping.

    2. Write all the output to *.cvs so it can be opened in excel.
    Each successfull ping is writed on one line, seperated with a ;

    This is supoused to run on a sun os machine, with perl 5 installed on it.(i dont have right now the permissions to write this script on the machine, but thats a question to put to the owners of the machine).

    Can any one comment on this script?

    thank you!

    Reply
  • francisco

    arghhh…
    sry for the dubble post… something wrong with the cashe of this computer..

    Reply
  • nolanguages

    Hi, I’m new to scripting and need a way to ping host. I have tried your script and receiving an error. Could you help me sort this out?
    My text editor is Padre, if that matters any. Unrecognized character x93; marked by <– HERE after print <– HERE near column
    7 at pinghosts2.pl line 8.

    Reply
    • thepuregeek

      Looks like during the copy and paste you are bringing over weird characters. Try typing the script instead.

      Reply

Leave a Reply

Comment
Name*
Mail*
Website*