2017-09-26 99 views
0

我幾個月來一直在使用這個小腳本,並取得了成功。今天,我知道有一個輸出它似乎無法趕上,屏幕出現空白了新的提示:AWK捕捉正則表達式

[email protected] ~]$ myscan ipsFile 23 

[email protected] ~]$ 

下面是代碼

#!/bin/bash 

sudo nmap -v -Pn -p T:$2 -reason -i $1 | awk ' { 

     if (/syn-ack/) { 
       print "Yes" 
       c++ 
     } 

     else if (/no-response|reset|host-unreach/) { 
       print "No" 
       c++ 
     } 
} 

END { print c} ' 

如果我運行NMAP對IP地址的一個那麼它會返回

Starting Nmap 5.51 (http://nmap.org) at 2017-09-26 11:44 CDT 
Initiating Parallel DNS resolution of 1 host. at 11:44 
Completed Parallel DNS resolution of 1 host. at 11:44, 0.00s elapsed 
Initiating Connect Scan at 11:44 
Scanning 1.1.1.1 [1 port] 
Completed Connect Scan at 11:44, 0.20s elapsed (1 total ports) 
Nmap scan report for 1.1.1.1 
Host is up, received user-set (0.20s latency). 
PORT STATE SERVICE REASON 
23/tcp filtered telnet host-unreach 

Read data files from: /usr/share/nmap 
Nmap done: 1 IP address (1 host up) scanned in 0.26 seconds 

我該如何捕捉'主機未完成'部分?

回答

0

你試過管道標準錯誤到標準輸出,就像

#!/bin/bash 

sudo nmap -v -Pn -p T:$2 -reason -i $1 2>&1 | awk ' { 

    if (/syn-ack/) { 
      print "Yes" 
      c++ 
    } 

    else if (/no-response|reset|host-unreach/) { 
      print "No" 
      c++ 
    } 
} 

END { print c} ' 
+0

謝謝,試過你的代碼片段,但出來的是相同的,沒有輸出。 –

+0

其實我發現如果我嘗試從nmap輸出中匹配單詞「filtered」,那麼它確實有效。試圖匹配「未完成」部分不起作用。 –

1

讓我們嘗試和調試這一點。執行此:

nmap -v -Pn -p T:23 -reason -i ipsFile | awk '{print $0}/syn-ack/{print "Yes";c++}/no-response|reset|host-unreach/{print "No";c++}END {print c}' > out.txt 

這裏唯一的區別是,awk腳本打印$ 0(即您的通話NMAP的輸出)到文件out.txt。嘗試grep您的unreach值。

我自己試了一下,發現我沒有host-unreach,我得到了net-unreach。在你的情況可能是同樣的事情。