Location: PHPKode > scripts > Simple Scan > SimpleScan.class.php
<?PHP
/*
* Simple Scan class (c) 2003 Michal Cumpl <hide@address.com>
*
*	Description: Simple scanner class
* 
*	
*	Constructor
*
*	SimpleScan(" correct IP address "," connection time out in s")
*	
*	
*	Functions
*
*	portInfo(" port number ")
*	- scan given port and store results in object variables
*
*	mtime()
*	- return current timestamp with microseconds
*	
*	Variables
*
*	$ip ... Scanned IP adderess
*	$port ... currently scanned port
*	$timeout ... port connection timeout 
*	$numports ... number of scanned ports
*	$openports ... number of open ports
*	$portstatus ... current port status (true/false)
*	$portresponse ... current port info (errorstring or fgets output)
*	$starttime ... start timestamp
*	$actualtime ... current port scanning time 
*	$totaltime ... total time of scanning in s
*	
*
*	Example usage
*
*	$scan = new SimpleScan("127.0.0.1", 0.2);
*	echo "Now scanning IP address ".$scan->ip."<br>";
*	for($i=20 ; $i<=80 ; $i++)
*	{
*		$scan->portinfo($i);
*		echo $scan->port.", ".$scan->portresponse.", ".round($scan->actualtime,2)."s<br>";
*	}
*	echo "Number of scanned ports: ".$scan->numports."<br>";
*	echo "Number of open ports: ".$scan->openports."<br>";
*	echo "Total time: ".round($scan->totlatime,2)."<br>";
*/

class SimpleScan
{
	var $ip;
	var $port;
	var $timeout;
	var $numports;
	var $openports;
	var $portstatus;
	var $portresponse;
	var $starttime;
	var $actualtime;
	var $totaltime;
	
	function simpleScan($ip,$timeout)
	{
		$this->ip = $ip;
		if($timeout > 0.01 && $timeout < 10) $this->timeout = $timeout;
		else $this->timeout = 0.5;
		$this->numports = 0;
		$this->openports = 0;
		$this->starttime = $this->mtime();
		$this->actualtime = 0;
		$this->totaltime = 0;
	}
	
	function portInfo($port)
	{
		flush();
		$this->port = $port;
		$time = $this->mtime();
		$fp = fsockopen ("$this->ip", $this->port, $errno, $errstr, $this->timeout);
		if(!$fp)
		{
			$this->portstatus = false;
			$this->portresponse = $errstr." (".$errno.")";
		}
		else
		{
			if($this->port == 80)
			{
				fputs ($fp, "GET / HTTP/1.0\r\nHost: ".$this->ip."\r\n\r\n");
				for($i=0;$i<5;$i++)
				{ 
					$tmpresponse = fgets($fp,1024);
					if(ereg("Server",$tmpresponse)) $this->portresponse = $tmpresponse;
				}
			}
			else
			{
				$this->portresponse = fgets($fp,1024);
			}
			$this->portstatus = true;
			$this->openports++;
			fclose($fp);
		}
		$this->actualtime = $this->mtime() - $time;
		$this->totaltime = $this->mtime() - $this->starttime;
		$this->numports++;
	}
	
	function mtime()
	{ 
    	list($usec, $sec) = explode(" ",microtime()); 
    	return ((float)$usec + (float)$sec);
    }
}

?>
Return current item: Simple Scan