Nmap\Nmap::scan PHP Method

scan() public method

public scan ( array $targets, array $ports = [] ) : Host[]
$targets array
$ports array
return Host[]
    public function scan(array $targets, array $ports = array())
    {
        $targets = implode(' ', array_map(function ($target) {
            return ProcessUtils::escapeArgument($target);
        }, $targets));
        $options = array();
        if (true === $this->enableOsDetection) {
            $options[] = '-O';
        }
        if (true === $this->enableServiceInfo) {
            $options[] = '-sV';
        }
        if (true === $this->enableVerbose) {
            $options[] = '-v';
        }
        if (true === $this->disablePortScan) {
            $options[] = '-sn';
        } elseif (!empty($ports)) {
            $options[] = '-p ' . implode(',', $ports);
        }
        if (true === $this->disableReverseDNS) {
            $options[] = '-n';
        }
        if (true == $this->treatHostsAsOnline) {
            $options[] = '-Pn';
        }
        $options[] = '-oX';
        $command = sprintf('%s %s %s %s', $this->executable, implode(' ', $options), ProcessUtils::escapeArgument($this->outputFile), $targets);
        $this->executor->execute($command);
        if (!file_exists($this->outputFile)) {
            throw new \RuntimeException(sprintf('Output file not found ("%s")', $this->outputFile));
        }
        return $this->parseOutputFile($this->outputFile);
    }

Usage Example

コード例 #1
0
ファイル: NmapTest.php プロジェクト: willdurand/nmap
 public function testScanSpecifyingPorts()
 {
     $outputFile = __DIR__ . '/Fixtures/test_scan_specifying_ports.xml';
     $expectedCommand = sprintf("nmap -p 21,22,80 -oX '%s' 'williamdurand.fr'", $outputFile);
     $executor = $this->getProcessExecutorMock();
     $executor->expects($this->at(1))->method('execute')->with($this->equalTo($expectedCommand))->will($this->returnValue(0));
     $nmap = new Nmap($executor, $outputFile);
     $hosts = $nmap->scan(array('williamdurand.fr'), array(21, 22, 80));
     $this->assertCount(1, $hosts);
     $host = current($hosts);
     $this->assertEquals('204.232.175.78', $host->getAddress());
     $this->assertEquals(Host::STATE_UP, $host->getState());
     $hostnames = $host->getHostnames();
     $this->assertCount(2, $hostnames);
     $this->assertEquals('williamdurand.fr', $hostnames[0]->getName());
     $this->assertEquals('user', $hostnames[0]->getType());
     $this->assertEquals('pages.github.com', $hostnames[1]->getName());
     $this->assertEquals('PTR', $hostnames[1]->getType());
     $ports = $host->getPorts();
     $this->assertCount(3, $ports);
     $this->assertEquals(21, $ports[0]->getNumber());
     $this->assertEquals('ftp', $ports[0]->getService()->getName());
     $this->assertEquals(22, $ports[1]->getNumber());
     $this->assertEquals('ssh', $ports[1]->getService()->getName());
     $this->assertEquals(80, $ports[2]->getNumber());
     $this->assertEquals('http', $ports[2]->getService()->getName());
 }