AWK正常表达

时间:2017-09-26 16:57:39

标签: regex awk

我几个月来一直在使用这个小脚本并取得了成功。今天我意识到有一个输出似乎无法捕获,屏幕显示为空白并带有一个新提示:

user@computer ~]$ myscan ipsFile 23

user@computer ~]$

这是代码

#!/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} '

如果我针对其中一个IP运行nmap,则返回

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

我怎样才能抓住“主持人 - 未达到的”#39;部?

2 个答案:

答案 0 :(得分: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。在你的情况下可能是同样的事情。

答案 1 :(得分:0)

您是否尝试将stderr管道输送到stdout,如

#!/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} '