1

I am trying to combine a FOR loop (that iterates over IP addresses) and an IF loop (that uses nc to check for a successful ssh connection before moving on).

I have an array ${INSTANCE_IPS[@]} with the IP addresses in it (at the moment it contains 2 IP Addresses). Here is the code:

while [ $ITERATION -le 30 ]
do
    for instance in ${INSTANCE_IPS[@]}
    do    
        nc -w 2 $instance 22 > /dev/null
            if [ $? -eq 0 ]
            then echo "connection succeeded to $instance"
            else
                ITERATION=$((ITERATION+1))
                echo ITERATION=$ITERATION
                echo "[info] connection to $instance unsuccessful. trying again. iteration=$ITERATION"
                sleep 20
            fi
    done
done

The 'else' statement in the IF loop works fine. It is the 'then' statement I am having problems with... I don't know how to break out of the IF loop once the connections are successful. Here's an example output when I run the above:

connection succeeded to 10.11.143.171
connection succeeded to 10.11.143.170
connection succeeded to 10.11.143.171
connection succeeded to 10.11.143.170
connection succeeded to 10.11.143.171
connection succeeded to 10.11.143.170

If I use break after then echo "connection succeeded to $instance then it only iterates through 1 IP address and never breaks out:

connection succeeded to 10.11.143.171
connection succeeded to 10.11.143.171
connection succeeded to 10.11.143.171

Ideally I think the best thing to do would be to query the number of elements in the array, then perform a netcat connection an increment some value by 1 until it equals the number of elements in the array, but I'm really not sure how to dot that.

Any help is appreciated :) Please let me know if you need any more information.

Cheers

4
  • do you want to break both loops - stop as soon as you find one working? If so you could set $ITERATION to 30 and then call break Commented Apr 23, 2014 at 1:35
  • 2
    By "IF loop" I assume you mean "IP loop"? But maybe you don't. There's no such thing as an "if loop". Commented Apr 23, 2014 at 1:36
  • @ Jerry - So I want to make sure a successful ssh connection is made to all elements (IP addresses) in the array. So if there's 2 IP addresses I want to make sure that two successful connections (one to each IP address) is made before exiting the for loop and continuing on with the bash script Commented Apr 23, 2014 at 1:39
  • @ ooga - Nah I just meant "IF". Sorry I for the terminology wrong. I just meant I am nesting IF in a FOR loop. Commented Apr 23, 2014 at 1:40

1 Answer 1

6

Reformulate your logic. You can't break if something succeeds, because you don't know whether another item might fail.

Instead, keep a flag saying whether you've successfully gone through all of them, and set it to false if something fails. At this point, you can also break and wait.

ITERATION=0
all_succeeded=false
while [ "$all_succeeded" = "false" -a $ITERATION -le 30 ]
do
    all_succeeded=true
    for instance in ${INSTANCE_IPS[@]}
    do    
        nc -w 2 $instance 22 > /dev/null
        if [ $? -eq 0 ]
        then 
          echo "connection succeeded to $instance"
        else
          all_succeeded=false
          echo "[info] connection to $instance unsuccessful."
          sleep 20
          break
        fi
    done
    let ITERATION++
done

if [ "$all_succeeded" = "true" ]
then
  echo "It worked"
else
  echo "Giving up"
fi
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.