From 2f66ca63f30dc8c758ed4a66247482872528255d Mon Sep 17 00:00:00 2001 From: Wouter van Dongen Date: Thu, 24 Jan 2019 23:23:57 +0100 Subject: [PATCH] Parse existing output file --- README.md | 6 ++++++ src/Nmap/Nmap.php | 15 ++++++++++----- tests/Nmap/Tests/NmapTest.php | 9 +++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 45ccc59..2aaf05a 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,12 @@ $nmap ->scan([ 'williamdurand.fr' ]); ``` +You can parse an existing Nmap XML output file with `parseOutputFile()`: + +``` php +$nmap->parseOutputFile($xmlFile); +``` + Installation ------------ diff --git a/src/Nmap/Nmap.php b/src/Nmap/Nmap.php index 4e56b33..537bdfd 100644 --- a/src/Nmap/Nmap.php +++ b/src/Nmap/Nmap.php @@ -116,10 +116,6 @@ public function scan(array $targets, array $ports = array()) $this->executor->execute($command, $this->timeout); - if (!file_exists($this->outputFile)) { - throw new \RuntimeException(sprintf('Output file not found ("%s")', $this->outputFile)); - } - return $this->parseOutputFile($this->outputFile); } @@ -207,8 +203,17 @@ public function setTimeout($timeout) return $this; } - private function parseOutputFile($xmlFile) + /** + * @param string $xmlFile + * + * @return Host[] + */ + public function parseOutputFile($xmlFile) { + if (!file_exists($xmlFile)) { + throw new \RuntimeException(sprintf('Output file not found ("%s")', $xmlFile)); + } + $xml = simplexml_load_file($xmlFile); $hosts = array(); diff --git a/tests/Nmap/Tests/NmapTest.php b/tests/Nmap/Tests/NmapTest.php index 337bf43..6d154da 100644 --- a/tests/Nmap/Tests/NmapTest.php +++ b/tests/Nmap/Tests/NmapTest.php @@ -267,6 +267,15 @@ public function testExecutableNotExecutable() new Nmap($executor); } + public function testExistingXmlOutputFileCanBeParsed() + { + $nmap = new Nmap($this->getProcessExecutorMock()); + $hosts = $nmap->parseOutputFile(__DIR__ . '/Fixtures/test_scan.xml'); + $host = current($hosts); + $this->assertCount(1, $hosts); + $this->assertCount(5, $host->getPorts()); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject | \Nmap\Util\ProcessExecutor */