2013-10-15 28 views
0

我使用nmap掃描網絡中的可用IP,我想僅使用PHP只掃描IP地址並僅保存陣列中的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做什麼?

+0

'nmap -oX'會給你一個很好的XML格式,這可能更容易(也更可靠)檢查。 (xpath:'// host [status [@ state =「up」]]/address/@ addr') – Wrikken

回答

1

你可以做到以下幾點:

$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!

+0

由於在日誌文件中有一些行不包含IP,並且您的代碼將這些字包含在數組中。如何添加條件去剝離不是IP地址的單詞而不添加到數組? – user2194507

+0

@ user2194507:你看過演示了嗎? –

+0

好吧,現在可以工作了,非常感謝。也感謝編輯我的帖子。 – user2194507

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" 
} 
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 

SAVE輸出到文件和讀取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);