use IO::Socket; use IO::Scalar; my $port = 80; print "Please enter an IP: "; my $ip = ; print "\nTrying $ip"; my $TIMEOUT = 3; # seconds eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $TIMEOUT; &CheckHost($ip, $port); alarm 0; }; if ($@) { if($@ eq "alarm\n") { die "Connection Timed out.\n"; } else { print "Socket Closed.\n"; } } sub CheckHost() { ($hostip, $hostport) = ($_[0], $_[1]); my $sock = new IO::Socket::INET ( PeerAddr => "$hostip", PeerPort => "$hostport", Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; chomp $hostport; print "Port $hostport is active on $hostip\n"; close($sock); }