1

I have an expect script that ssh's into a server to validate that the password that it is uses is correct. However, in the case it detects an incorrect password, although it does stop the expect command, it continues on with the rest of the script.

The code is below - my question is what command (if there is one) can we put in the fail case (incorrect password) and have it terminate the script so it doesn't continue with the rest of the functionality as if the password was correct.

expect <<-EOS
        #!/usr/bin/expect
        log_user 0
        set timeout $EXP_TIMEOUT
        puts "\nValidating Password...\n"
        spawn ssh -q -o ConnectTimeout=$SSH_TIMEOUT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${hostname}
        expect "*assword*"
        send -- "$secret\r"
        expect {
                "*assword*" {
                        send \x03
                        puts "\nIncorrect Password\n"
                }
                "$prompt" {
                        send -- "exit\r"
                        puts "\nPassword Validated\n"
                }
        }
        expect eof
EOS

Any help would be appreciated!

edit: I am using bash. The code pasted above is a function in an overall bash script.

3
  • Expect uses Tcl and Tcl also has exit. Apr 21, 2021 at 1:49
  • @Shak : You could return an exit code from your Tcl script to the invoking bash script, and from bash you could then evaluate this exit code and decided whether you want to terminate the bash script as well. Apr 21, 2021 at 7:59
  • @sexpect-ExpectforShells : This solves only half of the problem, as this terminates the Tcl process, but the OP also wants the calling bash process to exit. One somewhat brutal possibility would be to have the Tcl process kill its parent via the PPID, but I feel that having the parent decide this based on the Tcl exit code leads to code which is better maintainable. Apr 21, 2021 at 8:07

2 Answers 2

2

I added the following to my code and it seems to work. If anyone has any critiques, please feel free to let me know what could be improved.

expect command:

expect <<-EOS
        #!/usr/bin/expect
        log_user 0
        set timeout $EXP_TIMEOUT
        puts "\nValidating Password...\n"
        spawn ssh -q -o ConnectTimeout=$SSH_TIMEOUT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${hostname}
        expect "*assword*"
        send -- "$secret\r"
        expect {
                "*assword*" {
                        send \x03
                        puts "\nIncorrect Password\n"
                        exit 1
                }
                "$prompt" {
                        send -- "exit\r"
                        puts "\nPassword Validated\n"
                }
        }
        expect eof
        exit 0
EOS

Parent Bash Script:

if [ $? == 1 ] 
then 
echo "Password validation failed. Exiting..."
exit
fi
1

What coding language are you using? For many languages there is a simple code to exit the application such as this.Exit() in C# or exit() in python.

1
  • Hi @TechFocus , I am using Bash.
    – Shak
    Apr 21, 2021 at 0:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.