I have a list of IPs and I need to check them for opened ports using nmap.So far, my script is like this:#!/bin/bashfilename=$1port=$2echo STARTING NMAPwhile IFS= read -r linedo  nmap --host-timeout 15s -n $line -p $2  -oN output.txt | grep Discovered open port | awk {'print $6'} | awk -F/ {'print $1'} >> total.txtdone <$filenameIt works great but it's slow and I want to check, for example, 100 IPs from the file at once, instead of running them one by one.
bash multi-threads help require
bash;nmap;parallelism
Here's one way:#!/bin/bashfilename=$1port=$2echo STARTING NMAP## Read the file in batches of 100 linesfor((i=100;i<=$(wc -l < $filename);i+=100)); do     head -n $i $filename | tail -n 100 |        while IFS= read -r line        do          ## Launch the command in the background          nmap --host-timeout 15s -n $line -p $2  -oN output.txt |             grep Discovered open port | awk {'print $6'} |                 awk -F/ {'print $1'} >> total.txt &        done    ## Wait for this  batch to finish before moving to the next one    ## so you don't spam your CPU    waitdone 