Location: PHPKode > projects > Limph > limph-1.9.7/discovery.php
<?php
 /*
   Limph Is Monitoring Pingable Hosts
   Copyright (C) 2006 Jonathan Ciesla
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
require_once("config.php");
require_once("functionlib.php");
session_start();
favicon();

echo "<html><head><title>$sitetitle</title></head>";
echo "<body><h1>$sitetitle: Host Discovery</h1>";

$link = dbms_connect($dbhost, $dbuser, $dbpass, $dbname);

$user_id = dbms_escape_string($_SESSION['limphid']);

$admin_level =$_SESSION['limphadmin'];
authentication($admin_level,1);

echo "<link rel=stylesheet href=css.php>";

$subnet = dbms_escape_string($_POST['subnet']);
$fqdn = dbms_escape_string($_POST['fqdn']);

echo "<form action=univ.php method=post>";
echo "<input type=submit value=Back />";
echo "</form>";

echo "Enter 10.x.y for a Class A subnet | ";
echo "Enter 172.16-31.x for a Class B subnet | ";
echo "Enter 192.168.x for a Class C subnet<br /><br />";

echo "<form action=discovery.php method=post>";
echo "<input type=text name=subnet value=\"$subnet\">";
echo "<input type=submit value=Scan><br />";
echo "Fully qualified domain names?<input type=checkbox name=fqdn>";
echo "</form>";

echo "WARNING! Host discovery can be resource and time intensive.<br />";

$octets = explode(".", $subnet);

if(($octets[0]=="10")&&(!is_null($octets[1]))&&(!is_null($octets[2]))){
  $class = "A";
 };
if(($octets[0]=="172")&&($octets[1]>="16"&&$octets[1]<="31")&&(!is_null($octets[2]))){
  $class = "B";
 };
if(($octets[0]=="192")&&($octets[1]=="168")&&(!is_null($octets[2]))){
  $class = "C";
 };

if($class){
  echo "<table border=1 align=center>";
  echo "<form action=bulk_add.php method=post>";
  for($ip=1;$ip<=254;$ip++){
    $host = $octets[0] . "." . $octets[1] . "." . $octets[2] . "." . $ip;
    $status = udp_ping($host, 1);
    if($status==1){
      $hostname = gethostbyaddr($host);
      if($fqdn!="on"){
	$hostname_arr = explode(".", $hostname);
	$hostname = $hostname_arr[0];
      };
      $query = "SELECT COUNT(number) FROM hosts WHERE ip = '$host' AND port = '0';";
      $result = dbms_query($query);
      $line = dbms_fetch_array($result, "NUM");
      dbms_free_result($result);
      if($line[0]>=1){$bgcolor = "bgcolor=#A9A9A9";}else{$bgcolor = "";};
      echo "<tr><th $bgcolor>$host</th><th $bgcolor>$hostname</th>";
      if($line[0]>=1){
	echo "<th $bgcolor></th></tr>";
      } else {
	echo "<th $bgcolor>UDP<input type=checkbox name=udp[] value=\"$host\"></th></tr>";
      };
      for($port=1;$port<=1024;$port++){
	if(tcp_check($host, 1, $port)==1){
	  $query = "SELECT COUNT(number) FROM hosts WHERE ip = '$host' AND port = '$port';";
	  $result = dbms_query($query);
	  $line = dbms_fetch_array($result, "NUM");
	  dbms_free_result($result);
	  if($line[0]>=1){$bgcolor = "bgcolor=#A9A9A9";}else{$bgcolor = "";};
	  $service = getservbyport($port, "tcp");
	  $name = $host . ":" .$port;
	  echo "<tr><td align=center $bgcolor>$port</td><td align=center $bgcolor>$service</td>";
	  if($line[0]>=1){
	    echo "<td $bgcolor></td></tr>";
	  } else {
	    echo "<td align=center><input type=checkbox name=tcp[] value=\"$name\"></td></tr>";
	  };
	}; //tcp check
      }; //port range
    }; // udp ping
  }; // ip range
  echo "<tr><td colspan=3 align=center><input type=submit value=Add></td></tr>";
  echo "</form>";
  echo "</table>";
 }; //class

dbms_close($link);
echo "<br />";
echo "<form action=univ.php method=post>";
echo "<input type=submit value=Back />";
echo "</form>";

?>
Return current item: Limph