Search code examples
bashforkexpect

prevent expect from exiting on EOF


I have an expect script that connects to a vpn using openconnect. The script works perfectly, except that I don't know how to keep openconnect alive once the password has been provided and expect has reached the EOF. I think I need to fork the process, but I need it to keep the password somehow. Here is my script

#!/usr/bin/expect -f

spawn ./openconnect
expect "sudo"
send "sudo_password\r"
expect "password:"
send "vpn_password\r"
expect /Connected\stun1\sas/ #expect connected tun1 as some ip

and openconnect

#!/usr/bin/env bash

sudo -k
sudo -S openconnect --juniper --user username --csd-wrapper ~/juniper-vpn-py/tnc vpn_server

The output gets to connected tun1 as some ip as expected, but then expect closes and so does the process is spawned.


Solution

  • You have to wait for the spawned process to finish before exiting the Expect script or the spawned process may be killed prematurely. Try like this:

    expect "Connected tun1 as"
    expect -timeout -1 eof     ; # change the timeout value as needed
    

    or

    expect "Connected tun1 as"
    interact