3

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.

richbai90
  • 4,994
  • 4
  • 50
  • 85

1 Answers1

4

You have to wait for the spawned process to finish before exiting the Expect scirpt 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
pynexj
  • 19,215
  • 5
  • 38
  • 56
  • "interact" worked. "expect eof" didn't work with openconnect - it appears to get EOF after about 10 seconds, maybe openconnect closes stdin when launched from expect? – bain Aug 06 '20 at 11:44
  • 1
    sounds like it timed out waiting for the EOF. try `expect -timeout -1 eof` – pynexj Aug 06 '20 at 15:00
  • will the `openconnect` command keep running in foreground? or it'll exit soon and have its child process running in background? – pynexj Aug 06 '20 at 15:05
  • `openconnect` runs in foreground by default. It can be run in the background with `-b` but that leads to [this question](https://stackoverflow.com/questions/45578588/tcl-expect-kills-child-of-child-process) where the child process appears to be killed by SIGHUP even though `-ignore HUP` is used. – bain Aug 07 '20 at 09:24