2

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

192.168.1.2 443
192.168.2.100 8443
192.168.3.99 80
192.168.4.11 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 '18 at 19:09
  • Why not run nmap with the specific options you want per target as a separate scan? – schroeder Jul 13 '18 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 '18 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 '18 at 19:27
  • @game0ver any hint to develop that script? – ghostrider Jul 13 '18 at 19:29
0

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

#!/bin/bash

# declare an array for ip's
declare -a ips=("192.168.1.2" "192.168.2.100" "192.168.3.99" "192.168.4.11")

# 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++ ))
do
   nmap $ips[i] -p $ports[i]
done

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

3

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
0

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

#!/bin/bash

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

while read -r line 
do
    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.