2

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)

3 Answers 3

1

Try this awk oneliner:

nmap -Pn -oG - 192.168.1.1 | awk '/open/{ s = $2; for (i = 5; i <= NF-4; i++) s = substr($i,1,length($i)-4) "\n"; split(s, a, "/"); print $2 ":" a[1]}'
0

try one more solution with single awk only.

nmap -vv -iL file  | awk -F'[ /]' '/Discovered open port/{print $NF":"$4}'
-2

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.

1
  • I think it is overdose of grep and awk here. This could be done by single awk command too, I added it in here. Jul 22, 2017 at 19:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.