Dan Walker
Dan Walker

Reputation: 474

Expect script only gives output when using interact

I've run in to a bit of a weird issue with an expect script I'm writing. Basically, I have server A and server B. Server B is only accessible via telnet from server A (don't ask...).

This means, I SSH to server A, telnet to server B, and then run the command to get a load of information for a specified user. Now, the script works fine when I put 'interact' at the end, but when I remove it, the script no longer gives me output. Here is the rough script, it's just a proof-of-concept for now so ignore the fact there's no error/conditionals etc:

#!/usr/bin/expect -f
log_file "/tmp/temporarylog.log"

set timeout 30

set user [lindex $argv 0]

# connect to server A
send_user "Connecting to server A...\n";
spawn ssh user@servera.com

expect "assword: "
send "password123\n"

expect "# "
send "telnet serverb.com 6002\n"

expect "> "
send "login passwordabc\n"
send "user info $user\n"

# removing this line results in no output
interact

Running the script above gives me all of the user info. Removing 'interact' the script seems to telnet to serverb.com but then doesn't use 'login' or 'user info'.

The interact is the last part of the script, so it's confusing me as to why it's breaking things before it - this is my first expect script.

Any help appreciated.

Upvotes: 1

Views: 553

Answers (1)

glenn jackman
glenn jackman

Reputation: 247210

End your script with:

expect "> "
send "exit\r"
expect "# "
send "exit\r"
expect eof

Terminate your send strings with \r nor \n -- \r is carriage return, which is what hitting Enter sends.

Upvotes: 1

Related Questions