#!/usr/bin/perl use strict; use IO::Socket::INET; use Getopt::Long; my @hosts = ""; my $timeout = "2"; #my $port = ""; my @ports = ""; my $opts = GetOptions( "host=s" => \@hosts, "port=s" => \@ports, "timeout=s" => \$timeout ); @hosts = split /,/, join( ',', @hosts ); die "usage: sp -h [ -h ] -p [ -p ]\n" unless $hosts[1]; shift @ports; for my $host (@hosts) { next if $host eq ''; for my $port (@ports) { my $status = ( connection( $host, $port ) == 0 ) ? "Up" : "Down"; my $line = sprintf(" %-20s %-5s %-1s", $host, $port, $status); print $line . "\n"; } } ############## sub connection ############## { my $host = shift; my $tcp_port = shift; return ( IO::Socket::INET->new( Timeout => $timeout, PeerAddr => $host, PeerPort => $tcp_port, Proto => 'tcp' ) ) ? 0 : 1; } perl sp.pl -h google.com -h yahoo.com -p 80 google.com 80 Up yahoo.com 80 Up