-1

So I have my targets array which is a bunch of IP read from a file, now I am trying to cat a file that contains my IP and results from ssh command, ut because I am trying 3 different passwords on every target I will get 2 denials and one job scheduled result. So I figured, I will check if a job was scheduled for a particular result and I will print "$ip OK, if not then I will print "$ip failed" here is what I have :

#!/bin/bash
for ip in "${targets_array[@]}"
do
   cat "$output_file" | awk -v ip="$ip" '/$ip/&&/job/ {result="OK"} END {
     !result?result="failed":result=result;}'

done

Pseudocode of what I am trying to do:
1.cat from output file line of the file where you can find desired ip and the word "job" 
2.1. if you found it print append to a string variable "$ip OK"
2.2 if not found append to a string variable "$ip failed"
3. override output file with the result stored in the variable

but when I run it in debugging mode I see that the value was never passed to the awk script. How can I pass it?

+ awk -v ip= '/$ip/&&/job/ {result="OK"} END {
     !result?result="failed":result=result;}'

the file I am cutting from:

unwantedString 124.131.8010 Permission denied, please try again.
unwantedString 125.124.90.134 Permission denied, please try again.
unwantedString 145.120.100.8 Permission denied, please try again.
unwantedString 145.101.100.158 Permission denied, please try again.
unwantedString 124.131.80.2 Permission denied, please try again.
unwantedString 125.124.90.134 job 32 at 2020-12-16 23:30
unwantedString 145.120.100.8 Permission denied, please try again.
unwantedString 145.101.100.158 job 27 at 2020-12-16 23:30
unwantedString 124.131.8010 Permission denied, please try again.
unwantedString 125.124.90.134 Permission denied, please try again.
unwantedString 145.120.100.8 Permission denied, please try again.
unwantedString 145.101.100.158 Permission denied, please try again.
dwt.bar
  • 145
  • 1
  • 10

1 Answers1

2

First there is a error here : !#/bin/bash #!/bin/bash.

Then ip=$ip should be ip="$ip".

Then to use a variable you can call it without dollar sign ip but not within the pattern area which should be called $0 ~ ip.

Following your output :

#!/bin/bash
for ip in "${targets_array[@]}" 
do
ip="$ip"
awk -v ip="$ip" '{if ( $2 == ip && $3 == "job" )  {print $2 " success"} else {print $2 " failed"}}' output_file > out
done
Reda Salih
  • 1,754
  • Please share your output file content – Reda Salih Dec 16 '20 at 17:49
  • Applied those, still, the value is not passed. to the |awk -v ip="$ip", while debugging shows no value. when I echo this variable in the loop it exists, so the passing is the problem here. – dwt.bar Dec 16 '20 at 17:51
  • Share output_file content ? or an example ? – Reda Salih Dec 16 '20 at 17:54
  • /ip/ wont work within awk either, see the link steeldriver shared in the comments – jesse_b Dec 16 '20 at 17:57
  • @RedaSalih updated the question with the output file – dwt.bar Dec 16 '20 at 17:59
  • Mentioned or not it's not what he expect, these are my remarks. I know a variable should not be used in pattern area like /ip/ I wanted to mention that this is not variable expansion so i deleted $ sign. WE ARE HERE TO HELP, INSTEAD OF GIVING ME A DOWN VOTE, BETTER TO GIVE A SOLUTION. – Reda Salih Dec 16 '20 at 18:28