Exit status code for Expect from bash

Help
Sharjeel
2009-08-20
2013-05-02
  • Sharjeel
    Sharjeel
    2009-08-20

    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.

    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.

    This is the snippet in the bash script which calls the expect script.

    countu=0
    for servername in $(cat $linux_host_list)
    do
    ./script.expect $LUSERNAME $LPASS $servername Linux >> linux_log_file.txt & < /dev/null
    let countl=countl+1
    done

    and here is the expect script ( script.expect )snippet

    #!/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> /dev/null\r"
                            expect -re "$prompt"
                            log_user 0
                            send "exit\r"
                            expect -re "$prompt"
                            log_user 1
                            }

    I tried grabbing the bash command output ( $? ) 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.

     
    • ender
      ender
      2009-08-20

      You should try "expecting" the failed login message and then handle that. I'd do this whole thing in the expect script. Open the file and loop through it there and then when expect encountered the failed login message it could exit the loop.

      Or keep it this way but when you get the failed login message use the "exit 1" to return that failed status to bash.