Okay, Nmap doesn't directly take a file in the IP:PORT format per line to scan only that specific port on that specific IP in one go using just -iL. The -iL option feeds Nmap a list of targets (IPs, hostnames, networks), and then the -p option applies to all those targets.
So, you'll need a simple script (e.g., Bash, Python) to read your file and call Nmap for each IP:PORT pair.
#!/bin/bash
# Check if an input file was provided
if [ -z "$1" ]; then
echo "Usage: $0 <input_file_with_IP:PORT_pairs>"
exit 1
fi
INPUT_FILE="$1"
# Check if the input file exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: File '$INPUT_FILE' not found."
exit 1
fi
# Read the file line by line
while IFS= read -r line || [[ -n "$line" ]]; do
# Skip empty lines or lines starting with # (comments)
if [[ -z "$line" || "$line" =~ ^# ]]; then
continue
fi
# Split the line into IP and PORT
# Uses Parameter Expansion: ${variable%%pattern} removes longest suffix, ${variable##pattern} removes longest prefix
IP_ADDRESS="${line%%:*}"
PORT="${line##*:}"
# Validate if IP and PORT were correctly parsed (basic check)
if [[ -z "$IP_ADDRESS" || -z "$PORT" || "$IP_ADDRESS" == "$PORT" ]]; then
echo "Skipping invalid line: $line"
continue
fi
echo "----------------------------------------------------"
echo "Scanning IP: $IP_ADDRESS on Port: $PORT"
echo "----------------------------------------------------"
# Run nmap command
# -Pn: Don't ping, assume host is up (useful if ICMP is blocked but port is open)
# -sV: Version detection
# -p <port>: Specify port
# You can add other nmap options as needed, e.g.:
# -T4 for faster scan (be careful)
# -A for OS detection, version detection, script scanning, and traceroute
# -oN "output_${IP_ADDRESS}_${PORT}.txt" to save output for each scan
nmap -Pn -sV -p "$PORT" "$IP_ADDRESS"
# Example with output file:
# nmap -Pn -sV -p "$PORT" "$IP_ADDRESS" -oN "nmap_results/${IP_ADDRESS}_p${PORT}.nmap"
echo # Add a newline for better readability between scans
done < "$INPUT_FILE"
echo "----------------------------------------------------"
echo "All scans from $INPUT_FILE complete."
echo "----------------------------------------------------"
3. Make the script executable:
bash
chmod +x scan_pairs.sh
4. Run the script:
bash
./scan_pairs.sh targets.txt
(Optional: If you want to save results in separate files, uncomment and modify the -oN or -oA part in the script and create a directory like mkdir nmap_results first.)
Explanation of the Bash Script:
#!/bin/bash: Shebang, specifies the interpreter.
Input File Check: Ensures a filename is provided as an argument and that the file exists.
while IFS= read -r line || [[ -n "$line" ]]; do ... done < "$INPUT_FILE": This is a robust way to read a file line by line, even if the last line doesn't have a newline character.
IFS=: Prevents leading/trailing whitespace from being trimmed.
-r: Prevents backslash escapes from being interpreted.
Skipping Comments/Empty Lines: [[ -z "$line" || "$line" =~ ^# ]] && continue skips processing for empty lines or lines starting with #.
Splitting IP and Port:
IP_ADDRESS="${line%%:*}": Removes the longest match of :* from the end of $line. This effectively gets everything before the last colon.
PORT="${line##*:}": Removes the longest match of *: from the beginning of $line. This effectively gets everything after the last colon.
A basic validation checks if IP_ADDRESS or PORT is empty or if the split failed (e.g., no colon in the line).
echo "Scanning...": Prints information to the console about what's being scanned.
nmap -Pn -sV -p "$PORT" "$IP_ADDRESS":
nmap: The command itself.
-Pn: Treat all hosts as online -- skip host discovery. This is often crucial because if a host doesn't respond to ping (ICMP) but the specific port is open, Nmap might skip it without -Pn.
-sV: Probe open ports to determine service/version info.
-p "$PORT": Scans only the specified $PORT.
"$IP_ADDRESS": The target IP or hostname.
-oN "nmap_results/${IP_ADDRESS}_p${PORT}.nmap" (Example): If you uncomment this (and create the nmap_results directory), it will save the output of each scan to a uniquely named file in the "normal" Nmap format. You could use -oX for XML or -oA for all major formats.
Alternative (One-liner using awk and bash):
If you prefer a one-liner and don't need the script's robustness (like comment handling or detailed error checks), you can use awk to generate the commands and pipe them to bash:
'{print ...}': For each line, it prints an echo command for status and then the nmap command.
$1 is the IP (first field).
$2 is the Port (second field).
The output of awk (which is a series of shell commands) is then piped to bash for execution.
Choose the method that best suits your needs. The Bash script is more readable, maintainable, and easier to extend with more complex logic or error handling.