Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Ping sweep with reporting

by StarkRavingCalm (Sexton)
on May 14, 2007 at 20:15 UTC ( #615412=perlquestion: print w/ replies, xml ) Need Help??
StarkRavingCalm has asked for the wisdom of the Perl Monks concerning the following question:

I have been looking all over for a way to do this in Perl. I would like a simple perl script that will read a text file of hostnames and do ping sweeps against them. It should then send an alert email to a specific address if host is not reachable in xx seconds. Handle with care, I'm a Perl n00b. Thanks in advance

Comment on Ping sweep with reporting
Re: Ping sweep with reporting
by CountOrlok (Friar) on May 14, 2007 at 20:36 UTC
    Take a look at Net::Ping for pinging and MIME::Lite for mailing. They'll help you accomplish all this.
Re: Ping sweep with reporting
by kyle (Abbot) on May 14, 2007 at 20:41 UTC

    This sounds a lot like a job that is ordinarily performed with standard network monitoring applications (such as Nagios). Is there a reason you're rolling your own, or would an existing tool be able to solve your problem?

Re: Ping sweep with reporting
by StarkRavingCalm (Sexton) on May 14, 2007 at 20:56 UTC
    Nagios is a real bear to get going. This seems like an easy script for Perl. I am a complete beginner with Perl so I look it as a chance to learn.

      Just an FYI: Nagios as a VMWare appliance is pretty nice and easy to setup and is a nice introduction to vmware if you ever want to go that way. The appliance is completly free and last time I tried it, came with a nice configuration tool. http://www.entisys.com/nagiosvma/1.0/update.html


      ___________
      Eric Hodges
Re: Ping sweep with reporting
by shandor (Monk) on May 14, 2007 at 21:15 UTC
    Here's a possibility from a Solaris server. Note that by default, ping on Solaris returns "server is alive" when the server can ping. Otherwise, the ping times out after a few seconds.
    #!/usr/bin/perl use strict; use warnings; my $ping = "/usr/sbin/ping"; while (<DATA>){ chomp; my $ping_out = `$ping $_ 2> /dev/null`; chomp ($ping_out); if ($ping_out !~ /is alive/){ print "$_ isn't pinging\n"; } } __DATA__ server1 server2 server3
    Another possibility is to use the ping -t 1 format on a linux server. If anything shows up in $ping_out, then the server is pingable, otherwise it's not.
Re: Ping sweep with reporting
by StarkRavingCalm (Sexton) on May 15, 2007 at 14:40 UTC
    Here are my results (server names and IPs have been changed) root@stewie ~# perl pingsweep_pm.pl server1.domain.com isn't pinging server2.domain.com isn't pinging server3.domain.com isn't pinging root@stewie ~# ping server1.domain.com PING server1.domain.com (0.0.0.0) 56(84) bytes of data. 64 bytes from server1.domain.com (0.0.0.0): icmp_seq=0 ttl=62 time=0.199 ms 64 bytes from server1.domain.com (0.0.0.0): icmp_seq=1 ttl=62 time=0.263 ms 64 bytes from server1.domain.com (0.0.0.0): icmp_seq=2 ttl=62 time=0.198 ms Any ideas? Is there a perl module I am missing?
      Depending on the output from your ping program (/usr/sbin/ping), you may have to adjust the line:
      if ($ping_out !~ /is alive/){ print "$_ isn't pinging\n"; }
      to something like
      if ($ping_out !~ /bytes from/){ print "$_ isn't pinging\n"; }
Re: Ping sweep with reporting
by StarkRavingCalm (Sexton) on May 15, 2007 at 15:38 UTC
    That worked. Changing to "bytes from" and also changing to /bin/ping. I am not getting any output though.
      Try this:
      if ($ping_out !~ /bytes from/) { print "$_ isn't pinging\n"; } else { print "$_ is up\n"; }
Re: Ping sweep with reporting
by StarkRavingCalm (Sexton) on May 16, 2007 at 23:23 UTC
    Ok, so I have updated the script with the suggestions. I now looks like this:

    #!/usr/bin/perl use strict; use warnings; my $ping = "/bin/ping"; while (<DATA>){ chomp; my $ping_out = `$ping $_ 2> /dev/null`; chomp ($ping_out); if ($ping_out !~ /bytes from/) { print "$_ isn't pinging\n"; } else { print "$_ is up\n"; } __DATA__ SERVER01 SERVER02

    But.. I receive this when I run it:

    [root@stewie ~]# perl 615424.pl Missing right curly or square bracket at 615424.pl line 17, at end of +line syntax error at 615424.pl line 17, at EOF Execution of 615424.pl aborted due to compilation errors.

    Any clues??

    Edit: g0n - code tags

      Hard to tell since you didn't use <code></code> tags around it, but you're missing the } to close the while loop on or around line 17 of your source (before the DATA marker). Just like the error message says.

      Seems to be a lot of that going around today. Did someone declare today "'Ignore the simple error message which explicitly states what's wrong' Day" and I just didn't get the memo?

      Hi StarkRavingCalm,

      As Fletch says, it looks like you're simply missing the '}' where marked below:

      #!/usr/bin/perl use strict; use warnings; my $ping = "/bin/ping"; while (<DATA>) { chomp; my $ping_out = `$ping $_ 2> /dev/null`; chomp ($ping_out); if ($ping_out !~ /bytes from/) { print "$_ isn't pinging\n"; } else { print "$_ is up\n"; } # <--- You're missing a '}' right here to end the while { } loop __DATA__ SERVER01 SERVER02

      Was there some reason that "Missing right curly or square bracket" didn't lead you to that conclusion?

      And do you understand how to put <code> and </code> tags around your post so that it's readable?  If not, pay close attention when you hit "preview".  If it doesn't look right, don't click the "create" button until it does!  (You can continue clicking on "preview" until the post displays correctly).


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        Thanks for the help. I'm very new to Perl so some of this stuff can\will allude me unfortunaltly. I did see the error but what threw me off was the curly bracket right above where it said it was needed. And I'll pay more attention to my previews ; )

        Let me try adding the bracket and I'll post my results.
        Thanks!

Re: Ping sweep with reporting
by StarkRavingCalm (Sexton) on May 17, 2007 at 21:36 UTC
    Ok, here's what I have:
    #!/usr/bin/perl use strict; use warnings; my $ping = "/bin/ping"; while (<DATA>) { chomp; my $ping_out = `$ping $_ 2> /dev/null`; chomp ($ping_out); if ($ping_out !~ /bytes from/) { print "$_ isn't pinging\n"; } else { print "$_ is up\n"; } } __DATA__ myserver02

    And here's the output:
    root@stewie ~# perl pingsweep_pm.pl
    isn't pinging

    Thoughts?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://615412]
Approved by shigetsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-08-20 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (124 votes), past polls