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.


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 Jun 30 '16 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 '16 at 14:06
  • @NeilSmithline Good point, thank you. It looks like OP has already accepted, but I'll update for posterity.
    – h4ckNinja
    Jun 30 '16 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 '16 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. Jun 30 '16 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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