Search code examples
bashtclexpect

Interact not working from within Expect Script


I have the following expect script within a bash script and I'm unsure as to why the interact command is not working.

expect <<-EOS
    #!/usr/bin/expect
    set timeout $EXP_TIMEOUT
    send_user "\n The timeout being used is $EXP_TIMEOUT \n"
    send_user "\nLogging into remote host via SSH:\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 -- "/usr/seos/bin/sesu - $user\r"
                    expect "*assword*"
                    send -- "$secret\r"
                    expect "$prompt"
                    send -- "id\r"
                    expect "$prompt"
                    send -- "hostname -s\r"
                    interact
            }
    }
    expect eof

EOS Thank you all for your help!


Solution

  • interact can't let the user enter data via stdin because you are already redirecting stdin for the here document.

    Instead, you can save your here document with all expansions to a variable, and then pass that to -c. Here's a simplified example:

    script=$(cat << EOF
        spawn vi
        send "iHello $(hostname)"
        interact
    EOF
    )
    expect -c "$script"