I found this question and tried the solution given (accepted)

Can nmap take a list of ports to scan from a file?

However, it didn't really work as expected.

$ cat ports.list

Instead of scanning the ports in the file, nmap assume those ports as part of the host. See output below.

$ nmap -vvv -p `cat ports.list`
Starting Nmap
Initiating Ping Scan at 10:50
Scanning 4 hosts [2 ports/host]
Completed Ping Scan at 10:50, 1.22s elapsed (4 total hosts)
Nmap scan report for 22 ( [host down, received no-response]
Nmap scan report for 23 ( [host down, received no-response]
Nmap scan report for 25 ( [host down, received no-response]
Initiating Connect Scan at 10:50
Scanning localhost ( [1 port]
Completed Connect Scan at 10:50, 0.00s elapsed (1 total ports)
Nmap scan report for localhost (
Host is up, received conn-refused (0.000059s latency).
Scanned for 1s

21/tcp closed ftp     conn-refused

Read data files from: /usr/bin/../share/nmap
Nmap done: 4 IP addresses (1 host up) scanned in 1.28 seconds


Is it possible to take a list of ports to scan from a file?


Try a single line port specification without spaces, like such:

$ cat ports.list

That works as expected:

$ nmap -p `cat ports.list` host.example.com

Starting Nmap 7.60 ( https://nmap.org ) at 2021-03-25 15:07 UTC
Nmap scan report for host.example.com (
Host is up (0.00089s latency).

21/tcp closed ftp
22/tcp open   ssh
23/tcp closed telnet
25/tcp open   smtp

Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds

Note that in the answer you reference, he used 'tr' to translate one-port-per-line into single-line-comma-delimited.

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.