【问题标题】:AWK catching a regular expressionAWK 捕捉正则表达式
【发布时间】:2017-09-27 03:51:00
【问题描述】:

我已经成功使用这个小脚本几个月了。今天我意识到它似乎无法捕捉到一个输出,屏幕出现空白并出现新提示:

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

我怎样才能捕捉到“host-unreach”部分?

【问题讨论】:

    标签: regex awk


    【解决方案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 值。

    我自己尝试过,发现我得到的是net-unreach,而不是host-unreach。你的情况可能是一样的。

    【讨论】:

      【解决方案2】:

      您是否尝试过像管道一样将标准错误传送到标准输出

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

      【讨论】:

      • 谢谢,试过你的sn-p,结果还是一样,没有输出。
      • 实际上我发现如果我尝试从 nmap 输出中匹配单词“filtered”,那么它确实有效。试图只匹配“unreach”部分是行不通的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      相关资源
      最近更新 更多