$SIG{ "ALRM"} = sub { print "TIME_OUT! at $timeout\n"; exit; }; alarm ($timeout); #### require 5.002; require DBI; use strict; use Socket; use POSIX "sys_wait_h"; my ($pid, @port_range, @ip_list, $remote, $port, $timeout, $wait); #----------- config -----------# $timeout = 10; @port_range = qw (8080 6969 80 8081 8080 8000 3128 555 657 889 1180 1181 1182 1183 1184 1185 11012 25318 25719 6969 886); @ip_list = qw (196.2.49.19 grunt.mweb.co.za mweb.co.za); #this will be populated dynamically from a DB #----------- config -----------# foreach $remote (@ip_list) { foreach (@port_range) { if ($pid = fork) { $wait = wait (); print "Running with port $_ and \$pid $pid \$wait $wait \$? $? \n"; $port = $_; test ($remote, $port, $pid, $timeout); kill 9, $pid or die "KILL FAILED"; #not necessary? exit; } } } ########################################################### ## SUB ROUTINES ## ########################################################### sub test { my $remote = shift; my $port = shift; my $pid = shift; my $timeout = shift; my ($iaddr, $paddr, $proto); $SIG{"ALRM"} = sub { print "TIME_OUT! at $timeout\n"; exit; }; alarm ($timeout); $iaddr = inet_aton ($remote) or die "no host: $remote"; $paddr = sockaddr_in ($port, $iaddr); $proto = getprotobyname ('tcp'); socket (SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; if (connect (SOCK, $paddr)) { print "WE HAVE A CONNECTION!\n" } else { die "connect: $!"; } close (SOCK) or die "close: $!"; } exit;