Question

I am writing a bash with two ways and there is a problem:

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

I have two arguments: ports.txt ip.txt

cat ports.txt
80
443

cat ip.txt
1.1.1.1
2.2.2.2
3.3.3.3

I want to check each ip with all ports one by one..

any idea?

Than you

Was it helpful?

Solution

You can redirect each file on a different file descriptor:

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top