Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Ping sweep with reporting

by StarkRavingCalm (Sexton)
on May 14, 2007 at 20:15 UTC ( [id://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

Replies are listed 'Best First'.
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 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 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 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 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?
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

      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!

      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?

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-19 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found