/**
  * Check a range of ports at a machine
  *
  * This function can scan a range of ports (from $minPort
  * to $maxPort) on the machine $host for running services.
  *
  * @access public
  * @param  string Hostname
  * @param  integer Lowest port
  * @param  integer Highest port
  * @param  integer Timeout for socket connection in seconds (default is 30).
  * @return array  Associative array containing the result
  */
 function checkPortRange($host, $minPort, $maxPort, $timeout = 30)
 {
     for ($i = $minPort; $i <= $maxPort; $i++) {
         $retVal[$i] = Net_Portscan::checkPort($host, $i, $timeout);
     }
     return $retVal;
 }
<?php

/** $Id: 01-portscan.php,v 1.2 2003/04/22 12:03:35 mj Exp $ */
require_once "Net/Portscan.php";
/** Test for checkPort() and getService() */
if (Net_Portscan::checkPort("localhost", 80) == NET_PORTSCAN_SERVICE_FOUND) {
    echo "There is a service on your machine on port 80 (" . Net_Portscan::getService(80) . ").\n";
}
/** Test for checkPortRange() */
echo "Scanning localhost ports 70-90\n";
$result = Net_Portscan::checkPortRange("localhost", 70, 90);
foreach ($result as $port => $element) {
    echo "Port " . $port . ": ";
    if ($element == NET_PORTSCAN_SERVICE_FOUND) {
        echo " Service found.\n";
    } else {
        echo " No service found.\n";
    }
}
/** Test for getService() */
echo "On port 22, there service " . Net_Portscan::getService(22) . " is running.\n";
/** Test for getPort() */
echo "The finger service usually runs on port " . Net_Portscan::getPort("finger") . ".\n";
<?php

require_once "Net/Portscan.php";
##IP範圍
$IpRange = array('163.17.1.0' => '163.17.31.0', '120.110.1.0' => '120.110.31.0');
##IP範圍
##Port range
$PortRange = array('80', '443', '8080');
foreach ($IpRange as $startIP => $endIP) {
    $start = ip2long($startIP);
    $end = ip2long($endIP);
    foreach ($PortRange as $Port) {
        $web_server_ip_record = './webserver_port' . $Port . '_ip.txt';
        for ($d = $start; $d <= $end; $d++) {
            echo long2ip($d) . "<br />\n";
            $check_web_ip = long2ip($d);
            if (Net_Portscan::checkPort($check_web_ip, $Port, 1) == NET_PORTSCAN_SERVICE_FOUND) {
                echo "There is a service on your machine on port 80 (" . Net_Portscan::getService(80) . ").\n";
                file_put_contents($web_server_ip_record, $check_web_ip . "\n", FILE_APPEND | LOCK_EX);
            }
        }
    }
}