#!/usr/bin/env perl # # pingable - which of the words on stdin are hosts and which are pingable? # # $Id$ use strict; use warnings; use Socket qw(AF_INET); use Net::Ping; use Getopt::Std; my %opt; getopts( 'h?p:', \%opt ); ( $opt{h} || $opt{'?'} ) and HELP_MESSAGE(); ( defined( $opt{p} ) && $opt{p} !~ /^[1-9]\d*$/ ) and die "$0: option -pmust be followed by a valid port number\n"; sub HELP_MESSAGE { print STDERR <new( $opt{p} ? 'tcp' : 'icmp', 2 ); $opt{p} and $pinger->{port_num} = $opt{p}; while (<>) { foreach my $h ( grep { /./ } split( /\s+/, $_ ) ) { my $pingable = $pinger->ping($h); printf "%s,%s\n", $h, !defined($pingable) ? '?' : $pingable ? '+' : '-'; } }