6

Nmap can take a file containing a list of hosts to scan using the -iL flag. Is there similar functionality for a file containing a list of port numbers? I've scoured the man page, online docs at nmap.org and Googled, but nothing as of yet.

5 Answers 5

10

Not by itself, no. But with some quick scripting you can.

For example, on *nix systems:

nmap -p `cat ports.list` ...

This would allow you to maintain a list of ports in a file in a CSV format.

If you would prefer to have one port per line, you can do this (thanks @bonsaiviking) on *nix systems:

nmap -p $(tr '\n' , <ports.list)
4
  • 1
    Will this work on all platforms? Perhaps you want to make clear what the requirements are as the OP may be missing even basic scripting abilities Jun 30, 2016 at 0:53
  • 2
    The ports list needs to be separated by commas (,) not whitespace, so this would not work for a newline-delimited file. Try this instead: nmap -p $(tr '\n' , <ports.list) Jun 30, 2016 at 14:06
  • @NeilSmithline Good point, thank you. It looks like OP has already accepted, but I'll update for posterity.
    – h4ckNinja
    Jun 30, 2016 at 23:53
  • @bonsaiviking Yes, I did assume OP wanted it in a CSV. I'll update my answer with that. Thank you.
    – h4ckNinja
    Jun 30, 2016 at 23:54
2

Yes, it can, by using the targets-xml Nmap NSE script

Update: Actually, the functionality isn't yet in there, but it looks as if it is planned.

1

Yes, Nmap can take a file in the services file format with the --servicedb option. This also implies the -F option, meaning that only the services listed in that file will be scanned.* So just supply the services you want to scan in this format and you can accomplish this goal.

* The exception to this is if the file includes port frequency data like the nmap-services file bundled with Nmap. In that case, -F means to scan the 100 most-likely ports based on that frequency data.

1
  • thanks bonsaiviking, in particular I was looking for a way to take port numbers delimited by newlines from the file, having been output by another tool, so the services file format is not desirable in this case. Jun 30, 2016 at 7:57
0

Still no direct support from nmap for this option. If you have shell access and can write to a file then create one (here I named it port_list.txt) with a list of ports, one per line, and parse through it using a for loop like so;

for port in $(cat port_list.txt);do nmap -p$port HOSTorIP  ;done

Result would like so (here I`m scanning 127.0.0.1): enter image description here

0

Small tweak will do the job, by formating the cat output

$nmap -vv -p$(cat ports.txt| tr '\n' ',' | sed s/,$//)  10.10.10.10

assuming that ports.txt is line ordered like below:

$ cat ports.txt                         

53
88
135
139
389

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .