Search code examples
awknmap

Nmap output format ip:port


I need load ip list from file, scan it, and create output format such as ip:port. I tried this:

nmap -iL mylistwithip.txt -p 80,21 -oG -PS 80,21 | awk '/open/{print $2}' >` output.txt

but it gives me only "open" and that's all.

While I need only opened ports from list of IP addresses, for example:

192.168.2.1
192.168.2.2
192.168.2.3

after scan ports, sample output.txt:

192.168.2.1:80
192.168.2.1:21
192.168.2.3:80

(only scanned ip addresses with opened ports)


Solution

  • Quick and ugly hack to achieve that:

    nmap -vv -iL mylistwithip.txt  | grep "Discovered open port" | awk {'print $6":"$4'} | awk -F/ {'print $1'} > output.txt
    

    With -vv output includes lines like

    Discovered open port 22/tcp on 192.168.2.1
    Discovered open port 80/tcp on 192.168.2.1
    Discovered open port 22/tcp on 192.168.2.107
    Discovered open port 80/tcp on 192.168.2.107
    

    First awk selects "ip address" and "port number/protocol" fields, and second cuts off "/protocol".

    This will probably break in some future update of nmap. Using -sG (greppable output) would be a better idea.