I want to scan IP and port from a file as below: 443 8443 80 443

How can I do that? I know there is option to take IP from a file using -iL but how can do with IP and port as for each IP there is a specific port.

  • why not scan all 3 ports on every IP from the list?
    – schroeder
    Jul 13, 2018 at 19:09
  • Why not run nmap with the specific options you want per target as a separate scan?
    – schroeder
    Jul 13, 2018 at 19:11
  • @schroeder thanks. But I have very long list of IP and port. How can I do in a single shot?
    – ghostrider
    Jul 13, 2018 at 19:26
  • You could write a script to do that (launching separate nmap scans) since I think nmap doesn't support it...
    – game0ver
    Jul 13, 2018 at 19:27
  • @game0ver any hint to develop that script?
    – ghostrider
    Jul 13, 2018 at 19:29

3 Answers 3


You could only do it using a shell script, like this:


# declare an array for ip's
declare -a ips=("" "" "" "")

# declare an array for ports
declare -a ports=("443" "8443" "80" "443")

# now loop nmap through the above array combos
for (( i=0; i<=3; i++ ))
   nmap $ips[i] -p $ports[i]

Or you could do the same in C/C++ using the system command!


As of Nmap 7.70, this is not a feature that the Nmap development team is interested in developing. Full discussion can be found in Issue #1217, but the current best practice is to scan all desired ports against all targets.

EDIT: To everyone doing some big shell script to scan each port and host individually, you'll be here all day waiting for that to finish. Accumulate all the unique ports and targets and scan them in one go. You'll end up scanning a few ports on some systems that you didn't intend, but overall it won't take that long. And like everything else, it fits into one line of Perl:

perl -lanE'END{$,="\n";open$i,">ips";say$i keys%h;exec"nmap -iL ips -p".join(",",keys%p)}$h{$F[0]}=$p{$F[1]}=1' input.txt

I'd just wrap it in bash and call it good. Using the file format you specified above this would work.


if [ ! -f $1 ] 
    echo "Error: Must supply file"

while read -r line 
    host=`echo $line | cut -d" " -f1`
    port=`echo $line | cut -d" " -f2`
    echo "Scanning $host : $port " 
    nmap $host -p $port
done < $1

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.