sub PINGHOSTS {
  my $ipmask = shift;
  $ipmask =~ s/\*$/1-254/;
  my ($portmask, $cmd);
  my (@cmds, @output);
  
  if ($ipmask) {
    push(@cmds, $Nmapcmd." ".$nmap_general_options." ".$nmap_extra_options." -v -oM - -sP ".$ipmask." 2>/dev/null | grep \"Host: \"");
  } else {
    push(@cmds, $Nmapcmd." ".$nmap_general_options." ".$nmap_extra_options." -v -oM - -sP ".
                &GenIPMask()." 2>/dev/null | grep \"Host: \"");
  }
  
  foreach $cmd (@cmds) {
    &DEBUG("Running cmd: $cmd");
    @out = `$cmd`;
    chomp @out;
    push(@output, @out);
  }

  my $html = '<span style="'.$Fsn.'">Ping Sweep Results</span><table border=1><tr>'.
             '<td bgcolor="#333399" align="center" style="'.$Fsn.'">IP Address</td>'.
             '<td bgcolor="#333399" align="center" style="'.$Fsn.'">Host Name</td>'.
             '<td bgcolor="#333399" align="center" style="'.$Fsn.'">Alias</td>'.
             '<td bgcolor="#333399" align="center" style="'.$Fsn.'">Status</td>'.
             '<td bgcolor="#333399" align="center" style="'.$Fss.'">Currently<br>Monitored?</td>'.
             '</tr>';
  my ($host,@rest,$ip,$status,$monitored,$alias,$name);
  foreach (@output) {
    ($host, @rest) = split /\t/;
    $host =~ s/^Host: //;

    ($ip, $name) = split(/ /, $host);
    $name =~ s/\(|\)//g;
    unless ($ip) { $ip = $name; $name = ""; }

    $status = ($rest[0] =~ /^Status: Down/)?'<font color="#cc0000">Down</font>':'<font color="#00cc00">Up</font>';
    $alias = $monitored = "";
    if (exists $Config{$ip}) {
      $alias  = $Config{$ip}{'alias'};
      $name ||= $Config{$ip}{'name'};
      $monitored = 'Yes';
    }
    $alias     ||= '&nbsp;';
    $name      ||= '&nbsp;';
    $monitored ||= '<font color="#cc0000"><b>No</b></font>';

    $html .= '<tr><td align="right" style="'.$Fss.'">'.((exists $Config{$ip})?'<a href="'.$nPulseurl.'?a=detail&site='.$ip.'">'.$ip.'</a>':$ip).'</td>'.
                 '<td align="right" style="'.$Fss.'">'.$name.'</td>'.
                 '<td style="'.$Fss.'">'.$alias.'</td>'.
                 '<td align="center" style="'.$Fss.'">'.$status.'</td>'.
                 '<td align="center" style="'.$Fss.'">'.$monitored.'</td></tr>';
  }
  $html .= '</table>';
  return $html;
}

sub PORTSWEEP {
  my($ipmask,$port) = @_;
  $port =~ s/ //gs;
  $ipmask =~ s/ //gs;
  $ipmask =~ s/\*$/1-254/;

  if (($port != int($port)) or ($port <= 0) or ($port >= 65535) or ($ipmask =~ /[^0-9\.-]/)) {
    return '<span style="'.$Fsn.'">Invalid Address ('.$ipmask.') or Port Value ('.$port.')</span>';
  }

  my ($portmask, $cmd);
  my (@cmds, @output, @ports);
  
  if ($ipmask) {
    push(@cmds, $Nmapcmd." ".$nmap_general_options." ".$nmap_extra_options." -v -oM - ".$NmapScan." -p ".$port." ".$ipmask." 2>/dev/null | grep \"Host: \"");
  } else {
    push(@cmds, $Nmapcmd." ".$nmap_general_options." ".$nmap_extra_options." -v -oM - ".$NmapScan.
                " -p ".$port." ".&GenIPMask()." 2>/dev/null | grep \"Host: \"");
  }
  
  foreach $cmd (@cmds) {
    &DEBUG("Running cmd: $cmd");
    @out = `$cmd`;
    chomp @out;
    push(@output, @out);
  }

  my $html = '<span style="'.$Fsn.'">Port '.$port.($Services{$port}?' ('.$Services{$port}.')':'').' Sweep Results</span><table border=1><tr>'.
             '<td bgcolor="#333399" align="center" style="'.$Fsn.'">IP Address</td>'.
             '<td bgcolor="#333399" align="center" style="'.$Fsn.'">Host Name</td>'.
             '<td bgcolor="#333399" align="center" style="'.$Fsn.'">Alias</td>'.
             '<td bgcolor="#333399" align="center" style="'.$Fsn.'">Status</td>'.
             '<td bgcolor="#333399" align="center" style="'.$Fss.'" nowrap>Port Currently<br>Monitored?</td>'.
             '</tr>';

  my ($host,@rest,$ip,$name,$alias,$status,$monitored);
  my $found = 0;
  foreach $line (@output) {
    $found = 1;

    ($host, @rest) = split(/\t/,$line);
    $host =~ s/^Host: //;

    ($ip, $name) = split(/ /, $host);
    $name =~ s/\(|\)//g;
    unless ($ip) { $ip = $name; $name = ""; }

    $status = ($rest[0] =~ / $port\/open/)    ?'<font color="#00cc00">Open</font>':
              ($rest[0] =~ / $port\/filtered/)?'<font color="#cccc00">Filtered</font>':
                                               '<font color="#cc0000">Closed</font>';
    $alias = $monitored = "";
    if (exists $Config{$ip}) {
      @ports = split(/\s*,\s*/,$Config{$ip}{'portlist'});
      $monitored = 'Yes' if (grep(/^$port$/,@ports));
      $alias  = $Config{$ip}{'alias'};
      $name ||= $Config{$ip}{'name'};
    }
    $alias     ||= '&nbsp;';
    $name      ||= '&nbsp;';
    $monitored ||= '<font color="#cc0000"><b>No</b></font>';

    $html .= '<tr><td align="right" style="'.$Fss.'">'.((exists $Config{$ip})?'<a href="'.$nPulseurl.'?a=detail&site='.$ip.'">'.$ip.'</a>':$ip).'</td>'.
                 '<td align="right" style="'.$Fss.'">'.$name.'</td>'.
                 '<td style="'.$Fss.'">'.$alias.'</td>'.
                 '<td align="center" style="'.$Fss.'">'.$status.'</td>'.
                 '<td align="center" style="'.$Fss.'">'.$monitored.'</td></tr>';
  }
  $html = '<span style="'.$Fsn.'"><b>No hosts '.($ipmask?'('.$ipmask.')':'').' responded with port '.$port.' open.</b></span>' unless $found;
  $html .= '</table>' if $found;
  return $html;
}
