Search code examples
globexpect

Expect not matching glob pattern


I have written a script to automate a login process, below is a portion of the code I am having trouble with.

spawn globalprotect connect --portal vpnconnect.myserver.com
expect { "1234myuser*:" } {
    send -- "\r"
}

The prompt when running the globalprotect connect --portal vpnconnect.myserver.com terminal output looks like this:

Retrieving configuration...                                            
vpnconnect.manchester.ac.uk - Enter login credentials
username(1234myuser):

At this stage I need to enter a newline (hence the send -- "\r"), however the glob pattern "1234myuser*:" does not match this sequence. below is the output from running expect -d ./myscript.sh

expect version 5.45.4
argv[0] = expect  argv[1] = -d  argv[2] = ./myscript.sh 
set argc 0
set argv0 "./myscript.sh"
set argv ""
executing commands from command file .manvpn.sh
spawn globalprotect connect --portal vpnconnect.myserver.com
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {141688}

expect: does "" (spawn_id exp4) match glob pattern "1234myuser*:" "? no
Retrieving configuration...                                            

expect: does "\r                                                                       \rRetrieving configuration...\r\n" (spawn_id exp4) match glob pattern "1234myuser*:" "? no
vpnconnect.myserver.com - Enter login credentials
username(1234myuser):
expect: does "\r                                                                       \rRetrieving configuration...\r\nvpnconnect.myserver.com - Enter login credentials\r\nusername(1234myuser):" (spawn_id exp4) match glob pattern "1234myuser*:" "? no
expect: timed out

N.B My username is obviously not 1234myuser, but is similarly a sequence of alphanumeric characters.

N.B I have also tried similar glob patterns such as *(1234myuser):", "*1234myuser*" to no avail.


Solution

  • Expect is using Tcl and in Tcl { "1234myuser*:" } is the same as " \"1234myuser*:\" ".

    You should just write:

    expect "1234myuser*:" {
        send -- "\r"
    }