-2

I wrote the following script:

#!/bin/bash

echo "Reading data - headers - both"

if [ $# -ne 3 ]; then
    echo "Usage: ./nmap <port-range> <ip-list> <d || h || b>"
    exit 1
fi

rm -f /tmp/right.txt 1>/dev/null 2>/dev/null
rm -f /tmp/wrong.txt 1>/dev/null 2>/dev/null

output=""
if [ $3 == h ]; then
    while read -r -u3 port; do
    while read -r -u4 ip; do
#       echo -en "\n$ip $port: "
        OUT=$( nmap -p "$port" --script=http-headers.nse "$ip" | awk 'NR>=7 && NR<=10')
     #   [[ $OUT == *Apache* ]] && $(echo -en "$ip  $port\n" >> /tmp/right.txt) || $(echo -en "$ip  $port\n" >> /tmp/wrong.txt)
        [[ $OUT == *Apache* ]] && output="$output `echo -en "\n$ip -------------------- $port "`" && echo -e "$output" | column -t >> /tmp/right.txt || output="$output `echo -en "\n$ip -------------------- $port "`" && echo -e "$output" | column -t >> /tmp/wrong.txt
    done 4< "$2"
    done 3< "$1"

    echo -e "$output" | column -t

elif [ $3 == d ]; then
    echo data
elif [ $3 == b ]; then 
    echo both
fi

I expect my output have two files:

cat right.txt
ip1 ..... port1
ip2 ..... port1
ip2 ..... port2
ip3 ..... port3
.
.
.

cat wrong.txt
ip1 ..... port1
ip2 ..... port1
ip2 ..... port2
ip3 ..... port3
.
.
.

but it doesn't work properly...

any idea?

Thank you in advance

MLSC
  • 5,872
  • 8
  • 55
  • 89
  • I would edit my post and put my real output – MLSC Feb 07 '14 at 08:54
  • Try `[[ $OUT == *Apache* ]] && output="$output `echo -en "\n$ip -------------------- $port "`" && echo -e "$output" | column -t > tee /tmp/right.txt /tmp/wrong.txt > /dev/null` – Jayesh Bhoi Feb 07 '14 at 09:19
  • @ JKB pardon..I would try it..but I would also so thankful if you edit my script amd modify it in answer part... – MLSC Feb 07 '14 at 09:21
  • i have updated your scripts is it working for you? – Jayesh Bhoi Feb 07 '14 at 09:26
  • @ JKB I am going to check – MLSC Feb 07 '14 at 09:32
  • @ JKB the output just show me the correct result...but right.txt and wrong.txt are empty..the std output just show me the right output – MLSC Feb 07 '14 at 09:40
  • ok you get correct result on stdout but not get that in file right? check my updated script – Jayesh Bhoi Feb 07 '14 at 09:53
  • @ JKB we are closing step by step...at the stdout i have just right output...and both right and wrong.txt i have right output... – MLSC Feb 07 '14 at 10:06
  • ok, please check your script again i have again modified it..it will done your job. – Jayesh Bhoi Feb 07 '14 at 10:07
  • please add in end of inner while loop `echo -e "$output" | column -t | tee /tmp/right.txt /tmp/wrong.txt >> /dev/null` it will works for you – Jayesh Bhoi Feb 07 '14 at 10:10
  • @JKB I am really thankful but the result is exactly the same as original post....pardon..please put your answer in answer part to get upvote or accept if needed.. – MLSC Feb 07 '14 at 10:14
  • `rm -f /tmp/right.txt 1>/dev/null 2>/dev/null` this is incorrect. You should probably remove the redirections altogether, or replace with a simple `: >/tmp/right.txt` which will fail if you do not have write access to this file. – tripleee Feb 07 '14 at 10:35

2 Answers2

1

please find updated answer as i modified the BMW's answer for you please check it.

   #!/bin/bash

    echo "Reading data - headers - both"

    if [ $# -ne 3 ]; then
        echo "Usage: ./nmap <port-range> <ip-list> <d || h || b>"
        exit 1
    fi

    join -j 2 $2 $1 > temp.txt

    headers() 
    {
         while read -r ip port
         do
            printf "ip: %s  port:%d \n" $ip $port
            OUT=$(nmap -p "$port" --script=http-headers.nse "$ip" | tac | awk -F: 'NR<=13&&/Apache/{print $2; exit}')   
            if [[ "$OUT" == *Apache* ]]; then
                echo $ip $port >> /tmp/right.txt
            else
                echo $ip $port >> /tmp/wrong.txt
            fi
         done < temp.txt
    } 

    case $3 in 
      "h") headers ;;
      "d") echo data;;
      "b") echo both;;
      "*") echo "wrong input"
           exit;;
    esac
Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • @ JKB we are really close but the right and wrong are still not correct :( – MLSC Feb 07 '14 at 10:20
  • @MortezaLSC can you give out put of my newly edited answer..i edite just second ago – Jayesh Bhoi Feb 07 '14 at 10:26
  • @ JKB I put the output in answerpart.see please – MLSC Feb 07 '14 at 10:38
  • @MortezaLSC above script working for me..also updated output in my answer – Jayesh Bhoi Feb 07 '14 at 11:05
  • @ JKB as you see your output repeated...why?..I am going to show you my real ideal output...(I checked your answer doesn't work :( ..I am really thankful) – MLSC Feb 07 '14 at 11:34
  • yes it's repeated,only checked for one ip so it as. – Jayesh Bhoi Feb 07 '14 at 11:35
  • @ JKB I put in original post my ideal answer... :( ... I am really thankful my friend – MLSC Feb 07 '14 at 11:37
  • see this link and answer of BMW..may be good (http://stackoverflow.com/questions/21597450/search-in-output-and-cat-from-specific-line-to-specific-line-and-search-again) – MLSC Feb 07 '14 at 11:43
  • @MortezaLSC welcome my friend..it's great you find your answer and i also tried my best to solve it but anyway thanks.. – Jayesh Bhoi Feb 07 '14 at 11:51
  • @ JKB you're welcome...If we can solve it in this way it is better to do what BMW said...but If find the best solution I would be thankful again.. :) – MLSC Feb 07 '14 at 12:04
  • @ JKB you see that link has some problems...but this way I think is better if we could solve it – MLSC Feb 07 '14 at 12:46
  • yes i see that..but from BMW answer `join` command not working and i test your solution from my side and it works for me why you get web address instead of ip address still it mystery. – Jayesh Bhoi Feb 07 '14 at 12:49
  • @ JKB ok .. my work is just with ip..but even when i put your last edited answer in answer part it also does't chek the `condition` correctly :( – MLSC Feb 07 '14 at 12:52
  • @MortezaLSC i can't get you which `condition` are you pointing? – Jayesh Bhoi Feb 07 '14 at 12:57
  • 1
    @MortezaLSC please find my modified BMW's answer. – Jayesh Bhoi Feb 07 '14 at 13:16
  • @ JBK excellent dear friend :)... so sweet ... I changed (--script=http-headers.nse "$ip" | tac | awk -F: 'NR<=13&&/Apache/{print $2; exit}')) to (--script=http-headers.nse "$ip" | awk 'NR>=7 && NR<=10')) and worked exactly what I want..thank you so much :) – MLSC Feb 08 '14 at 04:39
0

Your short-circuit logic is flawed. true && false || true && true will execute all four statements.

It's not clear why you think the output status of echo would indicate anything except success anyway.

Is this closer to what you mean?

output="$output `echo -en "\n$ip -------------------- $port "`"

[[ $OUT == *Apache* ]] && file=/tmp/right.txt || file=/tmp/wrong.txt

echo -e "$output" | column -t >>"$file"

This is still wrong because it will echo the accumulated output multiple times, but at least it should show you what needs to be changed (and also how to refactor your code to avoid repetitions).

I guess you actually want something like

[[ $OUT == *Apache* ]] && file=/tmp/right.txt || file=/tmp/wrong.txt
output="$output `echo -en "\n$ip -------------------- $port " | tee -a "$file"`"

except this doesn't run the copy in the file through column -t. But you can do that later, or add it here and avoid it later (you seem to be running it for all instances of the output in the end anyway).

tripleee
  • 175,061
  • 34
  • 275
  • 318