jsstuball
jsstuball

Reputation: 4911

Why does my expect script exit prematurely?

Here is my except script:

#!/usr/bin/expect
spawn openvpn --config peter.ovpn
expect -exact "Enter Private Key Password: "
send -- "mypassword\r"

I run it and see OpenVPN ask for my client password. But the script exits, apparently without ever sending the password. When I try with an incorrect password it is the same (no incorrect password message). It is also exactly the same result if I delete the send -- "mypassword\r" line from the end of the expect script.

It's my first expect script so probably my syntax is wrong. Or could it be that OpenVPN is kicking me off for using an expect script to connect?

Upvotes: 0

Views: 538

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

Your syntax is fine. The problem is the script has no more commands to run after you send the password, so the expect script exits and that kills openvpn.

What do you need to do after you send the password?

If you just need to keep openvpn running, then do this:

#!/usr/bin/expect
spawn openvpn --config peter.ovpn
expect -exact "Enter Private Key Password: "
send -- "mypassword\r"
set timeout -1
expect eof

-1 means "infinite", and expect eof means you are waiting for the spawned process to exit before the expect script can exit.

Upvotes: 3

Related Questions