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


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)
  • 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 Commented 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) Commented 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
    Commented 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
    Commented Jun 30, 2016 at 23:54

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.


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.

  • 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. Commented Jun 30, 2016 at 7:57

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 enter image description here


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

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

assuming that ports.txt is line ordered like below:

$ cat ports.txt                         


You must log in to answer this question.

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