在循环中读取两个列表并按整个列表检查一个

时间:2014-02-05 11:06:39

标签: bash loops heredoc nmap

我正在用两种方式写一个bash并且存在一个问题:

1)

#!/bin/bash              
if [ $# -ne 2 ]; then
    echo "Usage: ./nmap <port-range> <ip-list>"
    exit 1
fi

OUT=`nmap -p $1 --script=http-headers.nse $2 | tail -n 13 | head -n 1 | awk -F: '{print $2}'`

awk 'FNR==NR{a[$1];next}{ for(i=1;i<=NF;i++){ if($i in a) {print $i} } } ' $1 $2
    if [ $OUT == 'Apache' ]; then
        echo right
    else
        echo wrong
    fi

2)

#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: ./nmap <port-range> <ip-list>"
exit 1
fi

OUT=`nmap -p $1 --script=http-headers.nse $2 | tail -n 13 | head -n 1 | awk -F: '{print$2}'`

while IFS= read -r LINE; do           
grep "$LINE" $2                       
if [ $OUT == 'Apache' ]; then
   echo right
else
   echo wrong
fi

我有两个参数:ports.txt ip.txt

cat ports.txt
80
443

cat ip.txt
1.1.1.1
2.2.2.2
3.3.3.3

我想逐个检查每个ip的所有端口..

任何想法?

比你

1 个答案:

答案 0 :(得分:1)

您可以在不同的文件描述符上重定向每个文件:

while read -r -u3 port; do
    while read -r -u4 ip; do

        # do stuff with $port and $ip
        echo -n "$ip $port: "
        OUT=$( nmap -p "$port" --script=http-headers.nse "$ip" |
               tail -n 13 | 
               awk -F: '{print $2; exit}' )
        [[ $OUT == Apache ]] && echo right || echo wrong

    done 4< "$2"
done 3< "$1"