title: Exit status code for Expect script called from Bash
body: <p>I made a Bash script which uses an expect script to automate ssh logins.The script connects to multiple servers and runs some commands.
  The bash script prompts for login credentials once.</p>
  
  <p>I want to incorporate a feature wherein the script terminates if the login fails for the first server to avoid the script checking for next servers resulting in the user account getting locked. The account lockout happens for 3 consecutive login failures and the number of server the script tries to connect is more than 3.</p>
  
  <p>This is the snippet in the bash script which calls the expect script.</p>
  
  <pre><code>countu=0
  for servername in $(cat $linux_host_list)
  do
  ./script.expect $LUSERNAME $LPASS $servername Linux &gt;&gt; linux_log_file.txt &amp; &lt; /dev/null
  let countl=countl+1
  done
  </code></pre>
  
  <p>and here is the expect script (<code>script.expect</code>) snippet</p>
  
  <pre><code>#!/usr/bin/expect -f
  set timeout 30
  set username [lindex $argv 0]
  set SPASS [lindex $argv 1]
  set servername [lindex $argv 2]
  set case [lindex $argv 3]
  set prompt "(%|#|\\$|%\]) $"
  switch $case {
      Linux {
          log_user 0
          spawn ssh -o StrictHostKeyChecking=no $username@$servername
          expect  {
              "assword:" {
                  send "$SPASS\r"
                  expect -re "$prompt"
              }
              expect -re "$prompt"
          }
          send "sudo su -\r"
          expect {
              "assword:" { send "$SPASS\r" }
          }
          expect -re "$prompt"
          log_user 1
          send "opcagt -status ; opctemplate -l ; cat watch.cf 2&gt; /dev/null\r"
          expect -re "$prompt"
          log_user 0
          send "exit\r"
          expect -re "$prompt"
          log_user 1
      }
  </code></pre>
  
  <p>I tried grabbing the bash command output (<code>$?</code>) assuming that the bash command would return a non zero value if login fails for incorrect password in the expect script but that did not work out.
  Any suggestions would be much appreciated.</p>