#!/usr/bin/perl5.8.4
require 'config/paths.pl';
require 'perllib/getopts.pl';
require 'config/sara.cf';
Getopts("f:v");
$debug = "";
$debug = "Y" if $opt_v;
@TARGETS = "";
if ($#ARGV < 0 && ! $opt_f) {
  print "Usage fwping [-v] [-f filename] [ systems]\n";
exit;
}
$FW_TIMEOUT=3;
$fw_ports = "$ENV{FW_PORTS}" if $ENV{FW_PORTS};
$fw_ports =~ s/\,/ /g;
$debug = "ON" if $opt_v;
if ($opt_f) {
  if (-f $opt_f) {
    @TARGETS = "";
    $target_cnt = "";
    open(TARGETS, "$opt_f");
    while (<TARGETS>) {
      chomp;
      $TARGETS[$target_cnt++] = $_ if (! /^\#/);
    }
  } else {
    print "File, $opt_f, not found\n";
    exit;
  }
} else {
  @TARGETS = @ARGV;
}
$fork_max = $concurrent_processes + $concurrent_processes;
$fork_cnt = 0;
for (@TARGETS) {
  if (fork() == 0) {
  $target = $_;
  $alive  = "";
  $fw     = "";
  open(FWPING, "$TCP_SCAN -a -f 1 $target $fw_ports|");
  while (<FWPING>) {
    chomp;
    print "$target: $_\n" if $debug;
    $fw  = "but firewalled" if /timed/;    
    next if /timed/;
    next if /no route/i;
    next if /unreachable/;
    $alive="ON";
  }
    print "$target is alive $fw\n" if $alive;
    close (FWPING);
    exit;
} else {
  $fork_cnt++;
  if ($fork_cnt >= $fork_max) {
    sleep $FW_TIMEOUT;
    $fork_cnt = 0;
  }
} 
}
wait;
#sleep 5;
