バッシュマルチスレッド

バッシュマルチスレッド

開いているポートがあるかどうかを確認するために使用する必要があるIPのリストがありますnmap。これまで私のスクリプトは次のようになりました。

#!/bin/bash

filename="$1"
port="$2"
echo "STARTING NMAP"
while IFS= read -r line
do
  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 <"$filename"

うまくいきますが遅いです。たとえば、ファイル内の100個のIPを1つずつ実行するのではなく、一度に確認したいと思います。

ベストアンサー1

1つの方法は次のとおりです。

#!/bin/bash

filename="$1"
port="$2"
echo "STARTING NMAP"

## Read the file in batches of 100 lines
for((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
    wait
done 

おすすめ記事