heres a snippet of code which Ive cleaed up a bit - Ive posted earlier viersions of this elsewehere on perlmonks and added some of the suggestions that I received.
#!/usr/bin/perl -T
# mship.cgi
# Just add hosts ip addresses, fqdn, or just hostnames
# gives a simple status of hosts up or hosts down
# next step is to add logging. The aim is not
# a full featured network monitor, just a quickie list
# of whats up and whats down - Its probably not useful
# in a large environment...
# Ted Fiedler <fiedlert@gmail.com>
use strict;
use warnings;
use Net::Ping;
use CGI qw/:standard/;
use POSIX qw(strftime);
# Clean up our UNIX environment
# for more infor read perldoc perlsec
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my (@hosts, @uphosts, @downhosts);
if ( -e "ship.cfg" ) {
open (CFGFILE, "ship.cfg") || die $!;
while (my @TMP=<CFGFILE>) {
next if ( /#/ );
# Lets untaint the data after making sure it only matches
# word characters... For more info read perldoc perlsec.
foreach $_(@TMP) {
if ($_ =~ /^([\w.]+)$/) {
$_ = $1;
push (@hosts, $1);
} else {
next;
}
}
}
} else {
# or enter your hosts here...
# This is the most secure/preferred way to do this
@hosts = qw/sapdev0 sapprd0 saptst0 pcbackup vader/;
}
# I haven't decided what else to do here - it just semantics...
my @verbage=("are", "hosts", "are", "hosts");
# udp is default
# other values can be icmp (if youre root)
# or TCP, which is expensive on the wire
my $p = Net::Ping->new('udp');
# Iterate through my hosts and get their status
foreach my $host(@hosts) {
chomp($host); # Cleans up ship.cfg if you're using it
if ($p->ping($host)) {
push (@uphosts, $host); # its either UP
} else {
push (@downhosts, $host); # or its DOWN...
}
}
$p->close();
print header, start_html;
print "<tt>\n";
print "\n<center><h3>MSHIP - my status of host IP's</h3></center>\n";
my $now=strftime "%m/%d/%Y %H:%M:%S", (localtime);
print "<center>$now</center>\n";
print hr;
# Just cleaning up the verbage is are host hosts etc...
if (scalar(@downhosts) == 1 ) {
$verbage[2]="is";
$verbage[3]="host"
}
if (scalar(@uphosts) == 1 ) {
$verbage[0]="is";
$verbage[1]="host";
}
# We don't care about things that don't exist...
unless (scalar(@downhosts) == 0) { print p,"There $verbage[2] ",scalar
+(@downhosts)," $verbage[3] unreachable\n",br }
print "There $verbage[0] ",scalar(@uphosts)," $verbage[1] alive\n";
print p,"The following $verbage[1] $verbage[0] alive: \n",br;
foreach my $uitem (sort @uphosts) { # sort is for cleanliness...
print li("$uitem\n"),br;
}
# Again, we don't care about things that don't exist
unless (scalar(@downhosts) == 0) { print p,"The following $verbage[3]
+$verbage[2] unreachable: \n",br }
foreach my $ditem (sort @downhosts) {
print li("$ditem\n"),br;
}
print "</tt>\n";
print start_form,
p,submit('Refresh'),
end_html;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.