Jose Mendez
Jose Mendez

Reputation: 43

AWK catching a regular expression

I have been using this little script for months now with success. Today I realize there is one output it cant seem to catch, screen comes up blank with a new prompt:

user@computer ~]$ myscan ipsFile 23

user@computer ~]$

Here is the code

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

If I run the nmap against one of the IPs then it returns

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

How can I catch the 'host-unreach' portion?

Upvotes: 0

Views: 133

Answers (2)

Marc Lambrichs
Marc Lambrichs

Reputation: 2882

Let's try and debug this. Execute this:

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

The only difference here is that the awk script prints $0 (i.e. the output of your nmap calls) to file out.txt. Try to grep your unreach value.

I tried this myself and found that instead of a host-unreach I got a net-unreach. Might be the same thing in your case.

Upvotes: 1

John Diller
John Diller

Reputation: 65

Have you tried piping stderr to stdout like

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

Upvotes: 0

Related Questions