Multiple host ping sweep Perl script

I was tasked with verify host connectivity after some server patches were pushed out.  So instead of manually pinging each one, and I already had a text file with all the affected hostnames, I decided to put together a little script that would read from the text file and print out whether the host was up or down. 

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.

I usually output to a file so that I can grep for either successful or failed. 

Linux, Network Security, Perl Scripts, Tutorials/Whitepapers, Windows

22 comments to Multiple host ping sweep Perl script

  • triznay

    btw, the smily looking thing is acutally “;” followed by “)”.

    Reply
  • Daniel Valdes

    Great script. Thank you.

    Reply
  • triznay

    oh yea… btw, if you take out the “-xy” in the bang line on the starter shell script it doesn’t display the script every time it loops… I had originally put that in to trouble shoot another script and just left it in. It’s alot cleaner if it taken out.

    Reply
  • George

    Please can someone tell me where to write this perl code? how do i get te “program”

    Reply
  • triz

    George, you simple copy the above code ino two seperate scripts… one being a shell script and the other being a Perl script. Execute the shell scipt with loops the Perls script to accomplish the ping test. The directions surrounding the code should explain the rest… but feel free to ask more questions, hope this helped.

    Reply
  • ravi

    hey do wee need shell scripting to excute perl script in freebsd .. why did u created two scripts shell and perl why don’t you do entire thing in perl scripting? …

    Reply
    • triz

      Hi Ravi, yes I do use two scripts to run this… basically the shell script just kicks off the other script and makes it loop. I could convert the shell script into a perl script and I’m sure it wouldn’t be too difficult to combine both scripts into one.

      Reply
  • ravi

    my requirement is by using the perl language periodically query hosts in my domain and send an e-mail report if the host is down ? can u help me out in this issue ? andi have to log the information … thank you in advance .

    Reply
    • triz

      I would set up a crontab job to run how ever often you want and direct the output to a log location.

      It is also easy to add some lines of code that will email output that you specify from the script. I will try to update this script in near future as I have already made some enhancements to the original script.

      Reply
  • Isaac Michael

    Hello there, great script .
    I have couple of questions,
    – Have you made the change and combined both scripts in a single perl script?

    I created the host.txt file including 2 ip addresses (for testing)
    10.0.0.1
    10.0.0.2

    – in attempt to run the shell script the script seem to be hung…or in a loop of some sort,…

    ——————————————————————
    $ ./ping.sh
    #!/bin/bash -xv
    #
    #startPing.sh
    #This script starts the ping.pl perl script to read multiple ping
    #destinations from hosts.txt
    #written by trizsolo
    #~~~~~~~~~~~~~~~~
    for i in `cat hosts.txt`
    do
    ./ping.pl $i
    done
    cat hosts.txt
    ++ cat hosts.txt
    + for i in ‘`cat hosts.txt`’
    + ./ping.pl 10.0.0.1
    ——————————————————————-

    What am i doing wrong ?
    I have the same script in the same location
    – ping.pl
    – ping.sh
    -hosts.txt

    Please advise,
    Respectfully,

    Reply
    • triz

      Sorry for such a delayed response. I’ve been swamped meeting audit requirements for new policies/standards being pushed out… Let me have a little time … to find time to look into this properly.

      Reply
  • mithun

    how do we call multiple sessions of a script from command line in windows..

    for eg incase of linux i can follow the below method

    while($i)
    {
    system(“perl ping_script.pl ‘$ip’ ‘$flag’ ‘$name’ &”);
    }

    but does ‘&’ help me in windows ?

    Reply
    • triz

      That’s a good question, and I’m not sure I know the answer off the top of my head. I do know that when using && it allows user to run multiple commands on cmd.exe. This link below may help you set variables. However, Since my script above is Perl, I would recommend installing Active Perl and use the above example.

      http://technet.microsoft.com/en-us/library/cc737438%28WS.10%29.aspx

      Reply
      • mithun

        hey triz , really appreciate ur reply, thank u 🙂 .
        However there is another command that i suspect would really help me out in windows, i.e.

        we may possible be allowed to use this command

        while($i)
        {
        system(“wperl ping_script.pl ‘$ip’ ‘$flag’ ‘$name’ &”);
        }

        “wperl” usually helps run the script in the background

        Reply
      • mithun

        after a brief search came up with this solution and it works 😉

        while($i)
        {
        system(“start wperl ping_script.pl ‘$ip’ ‘$flag’ ‘$name’ ”);
        }

        so in *NIX … xxx &
        in dos/win … start xxx

        wperl helps running multiple scripts in the background simultaneously

        Reply
  • mithun

    Hi Triz,

    Ur suggestion would be highly appreciated.

    Used the following mechanism to ping multiple hosts, however the below loop exits even before all the hosts are completed.

    $i =0;
    foreach $ip_addr (@arr)
    {
    chomp($ip_addr);
    $pid = fork();
    if($pid Can’t fork! Bad kernel!”;
    print “$i – $ip_addr – $pid n”;
    }
    elsif($pid == 0)
    {
    $PING_STATUS{$ip_addr}=system(“/bin/ping -c1 -w1 $ip_addr > /dev/null”);
    print “$ip_addr – $PING_STATUS{$ip_addr} n”;
    exit(0);
    }

    $i++;
    }
    1 while (wait() != -1);

    Reply
  • thepuregeek

    I’ve consolidated the two scripts into one, and created a new post here > http://trizsolo.wordpress.com/2012/02/18/simple-perl-mu…g-sweep-script/

    Reply
    • fresh

      Hi Triz,

      Apparently the URL provided is not found. Can you please verify if this link is available.

      Thanks,

      fresh

      Reply
  • shahbaz Khan

    Nice Creativity

    Reply
  • Web Grabber

    I don’t even understand how I finished up right here, however I thought this publish was good. I don’t realize who you’re however definitely you are going to a famous blogger should you are not already. Cheers!

    Reply
  • http://fb-rocket.com/

    I visit each day a few sites and blogs to read posts, but this
    website provides feature based content.

    Reply

Leave a Reply

Comment
Name*
Mail*
Website*