See:
Testing for port connectivity.
JSchmitz shows you how to get the status
of a single port, and
McDarren shows how to handle multiple
hosts. Here's an example:
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket::PortState qw(check_ports);
my $proto = 'tcp';
my $port = '80';
my $address = '127.0.0.1';
my($section, $ping_timeout, %porthash);
$porthash{$proto}{$port}{'name'} = $section;
check_ports($address, $ping_timeout, \%porthash);
my $open = $porthash{$proto}{$port}{'open'};
if ($open) {
print "alive\n";
}
else {
print "dead\n";
}