Net::Ping doesn't use ICMP, but UDP/TCP to connect to the echo port of a machine. I tried using it, but found out you couldn't use ICMP and most machines don't have their echo port enabled for obvious reasons... In my current version I use Open3. The options for ping probably don't apply to Windows ping. I use FreeBSD 4.2 here.
#!/usr/local/bin/perl5 -w
use IPC::Open3;
use Symbol;
# .ping contains hostnames, each on a seperate line.
open(HOSTS, "<.ping") || die("Can't open hostsfile! ($!)\n");
@hosts = <HOSTS>;
close(HOSTS);
%PING = ();
%STATUS = (
-1 => "Host not found",
0 => "Not done",
1 => "Host is alive",
2 => "No reply from host",
);
foreach $host (@hosts) {
chomp $host;
$WTR = gensym();
$RDR = gensym();
$ERR = gensym();
$PING{$host}{'PID'} = open3($WTR, $RDR, $ERR, "/sbin/ping -n -q -c 1
+ $host");
close($WTR);
while (<$RDR>) { $PING{$host}{'OUT'} .= $_; }
while (<$ERR>) { $PING{$host}{'ERR'} .= $_; }
$PING{$host}{'STATUS'} = 0;
}
foreach $host (keys %PING) {
if(defined($PING{$host}{'ERR'})) {
if($PING{$host}{'ERR'} =~ /Unknown host/) {
$PING{$host}{'STATUS'} = -1;
next;
}
}
if($PING{$host}{'OUT'} =~ /.*, (.*) packets received.*/) {
$PING{$host}{'STATUS'} = ($1 == 1?1:2);
}
}
foreach $host (keys %PING) {
printf "%-30s %s\n", $host, $STATUS{$PING{$host}{'STATUS'}};
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.