0

我使用 nmap 扫描网络中的可用 IP,我想使用 PHP 仅扫描 IP 地址并仅将 IP 地址保存在数组中。

这是输出的文本文件。

Starting Nmap 6.40 ( http://nmap.org ) at 2013-10-15 22:07 SE Asia Standard Time
Nmap scan report for 110.77.144.25
Host is up (0.042s latency).
Nmap scan report for 110.77.144.27
Host is up (0.051s latency).
Nmap scan report for 110.77.144.88
Host is up (0.033s latency).
Nmap scan report for 110.77.144.90
Host is up (0.037s latency).
Nmap scan report for 110.77.144.91
Host is up (0.038s latency).
Nmap scan report for 110.77.144.92
Host is up (0.034s latency).
Nmap scan report for 110.77.144.93
Host is up (0.035s latency).
Nmap scan report for 110.77.144.137
Host is up (0.063s latency).
Nmap scan report for 110.77.144.139
Host is up (0.037s latency).
Nmap scan report for 110.77.144.145
Host is up (0.064s latency).
Nmap scan report for 110.77.144.161
Host is up (0.074s latency).
Nmap done: 256 IP addresses (42 hosts up) scanned in 14.44 seconds

我想像这样将输出保存在数组中

$available = array("110.77.233.1", "110.77.233.2", 
                   "110.77.233.3", "110.77.233.4",
                   "110.77.254.16");

我该如何使用 PHP?

4

3 回答 3

1

You can do the following:

$lines = file('file.txt');    
for ($i=1; $i <= count($lines); $i+=2) { 
    list($IP) = array_reverse(explode(' ', $lines[$i]));
    $available[] = $IP;
}
array_pop($available);
print_r($available);

Demo!

于 2013-10-15T17:57:49.947 回答
0

尝试这个:

nmap -v -sn 110.77.144.25-255 | grep ^Nmap | awk '{print $5;}' | grep ^[0-9].*

结果将是:

110.77.144.25
110.77.144.26
...
110.77.144.254
110.77.144.255

将输出保存到文件并从 PHP 中读取:

COMMAND:

nmap -v -sn 110.77.144.25-255 | grep ^Nmap | awk '{print $5;}' | grep ^[0-9].* > output.txt

PHP:

<?php
$fp = fopen('[path to file]/output.txt', 'r');
while(!feof($fp)) {
    $each_ip = fgets($fp, 4096);
    echo $each_ip;
}
fclose($fp);
于 2013-10-15T18:37:49.567 回答
0

的结构化输出-oX让生活变得简单:

$ nmap -sP -oX output.xml  10.60.12.50-59

Starting Nmap 6.00 ( http://nmap.org ) at 2013-10-15 20:09 CEST
Nmap scan report for 10.60.12.50-59
Host is up (0.000080s latency).
Nmap done: 10 IP addresses (1 host up) scanned in 1.41 seconds

$ php -r'$d = simplexml_load_file("output.xml");
> var_dump(array_map("strval",$d->xpath("//host[status[@state=\"up\"]]/address/@addr")));'
array(1) {
  [0] =>
  string(11) "10.60.12.59"
}
于 2013-10-15T18:12:08.690 回答