"Misses" in a loop



Hi all,

For a customer project I'm writing a "firewall proof" script.
This script, between two systems tries systematically all tcp ports (up to a given parameter) of all target network.

Now, I've made a double loop (one for the IP addresses, one for the ports) to browse this). For speed reasons, I fork all the network "clients" and they all individually log their results in a global log file opened before the fork.

Despite this, and the fact that I log both success and failure and that I check that all children successfully died, I get some holes the log files, means that some tests do not get logged (they most of the times do show up in the firewall log) and I get no error message telling me that a child unexpectedly died or crashed.

I don't show the "server" side activation, but the necessary listening daemons are dynamically launched on the target system. I also get logs on the target that prove this.

Any idea, the "core" of the script is attached below (some meaningless instruction about local or remote network interfaces activation/configuration/de-activation have been left aside, as well as debug stuff).

Thanks,

Jean-Charles

$SIG{CHLD}='IGNORE';
foreach my $source (@sources) {
$source =~ /^(eth\d+)#(\d+\.\d+\.\d+\.\d+)\/(\d+\.\d+\.\d+\.\d+)>(\d+\.\d+\.\d+\.\d+)$/;
my ($sourceif, $sourceip, $srcnetmask, $defsrcgw) = ($1, $2, $3, $4);
foreach (@targets) {
/^(eth\d+)#(\d+\.\d+\.\d+\.\d+)\/(\d+\.\d+\.\d+\.\d+)>(\d+\.\d+\.\d+\.\d+)$/;
my ($targetif, $targetip, $tgtnetmask, $deftgtgw) = ($1, $2, $3, $4);
next if (&samesubnet($sourceip, $targetip));
foreach my $port ( 1 .. $endport ) {
my $pid = fork();
$logfile->print("Error : Could not fork to test reachability of $targetip:$port from $sourceif#$sourceip") unless (defined $pid);
die "Cannot fork\n" unless (defined $pid);
if ( $pid ) {
$child{$pid}="$targetip:$port:$sourceip:$sourceif";
} else {
my $socket = IO::Socket::INET->new( PeerHost => $targetip,
PeerPort => $port,
Proto => "tcp",
Type => SOCK_STREAM,
Blocking => 1,
Timeout => $timeout );
if ($socket) {
$logfile->print("$targetip is reacheable on port $port from ip $sourceip on interface $sourceif.\n");
} else {
$logfile->print("$targetip is NOT reacheable on port $port from ip $sourceip on interface $sourceif.\n");
}
$logfile->flush();
$socket && usleep($closesleep);
$socket && $socket->close();
undef $socket;
exit;
}
}
usleep($childrensleep);
foreach (keys %child) {
delete $child{$_} unless (kill 0 => $_);
}
if (%child) {
usleep($childrensleep);
foreach (keys %child) {
if (kill 0 => $_) {
kill 9 => $_;
$child{$_} =~ /(.*):(.*):(.*):(.*)/;
$logfile->print("$1 is NOT reacheable on port $2 from ip $3 on interface $4 (timeout kill).\n");
}
delete $child{$_} unless (kill 0 => $_);
}
die "All children not dead\n" if (%child);
}
}
}


.



Relevant Pages

  • Re: bin/134694: gives false-positive when unable to obtain socket [WAS: sshd(8) - alert user
    ... would like to enable some form of console output when the rc.d script ... then you're spawning SSH on the same port via rc.d script; ... after step 2 there will be no SSH daemon ... the socket won't be bound, ...
    (freebsd-hackers)
  • Re: How to Create a TCP/IP PrinterPort on a MS Cluster 2k3 via Scr
    ... XP is not in the cluster so I'm not clear on the failure question. ... the user running the script should be admin on the remote print server. ... thus the creation of the port on the node. ...
    (microsoft.public.windows.server.clustering)
  • Re: How to Create a TCP/IP PrinterPort on a MS Cluster 2k3 via Scr
    ... XP is not in the cluster so I'm not clear on the failure question. ... the user running the script should be admin on the remote print server. ... thus the creation of the port on the node. ...
    (microsoft.public.windows.server.clustering)
  • Re: Script fails but individual commands run fine
    ... > another application which is listening on port 35000 and dials the ... If I open a tclsh and enter the script commands line ... My guess is that your tcl script sends $number to the socket, ... perhaps you are not connecting to another application on REMOTEHOST, ...
    (comp.lang.tcl)
  • Firewall Rules Summary
    ... Subject: Firewall Rules Summary ... This script is provided "as is" with no implied warranty. ... this came from various howtos and articles on iptables that existed around ... #specific port denies>1024 tcp ...
    (Focus-Linux)