title: How to check connection to a list of servers in bash?
body: <p>Im trying to check connections for a list of servers. I want to loop through the list, check if a connection works and if yes, do some stuff, if not, echo out a problem message.</p>
  
  <p>My problem is:<br/>
  the script stops at the first node without echoing the $?.
  So, whats wrong with my for-loop?</p>
  
  <p>These vars are included from a config file:<br/>
  <code>$nodes</code> is a list of server IPs like <code>1.1.1.1,2.2.2.2,10.10.10.10</code><br/>
  <code>$user</code> is one string</p>
  
  <pre><code>for node in $(echo $nodes | sed "s/,/ /g")
      do
      echo "Checking Node: $node"
  
      ssh -q -o ConnectTimeout=3 $user@$node echo ok
      echo $?
      if [[ $? != 0 ]]
      then
          echo "Problem in logging into $node"
      else
          # do some stuff here
      fi
  done
  </code></pre>
  
  <p><strong>EDIT #1:</strong><br/></p>
  
  <pre><code>for node in $(echo $nodes | sed "s/,/ /g")
      do
      echo "Checking Node: $node"
  
      ssh -q -t -o ConnectTimeout=3 $user@$node "echo ok"
      retcode=$?
      echo $retcode
      if [[ "$retcode" -ne 0 ]]
      then
          echo "Problem in logging into $node"
      else
          echo "OK"
      fi
  done
  </code></pre>