regexexpect

RegEx in Expect Script to match colon and questionmark followed by one whitespace


This is not working:

#!/usr/bin/env expect

spawn -noecho /scripts/clone_repos.sh

expect {(yes/no/[fingerprint])?}
send "yes\r"

expect {':}
send "admin\r"

expect {':}
send "superComplexPassword\r"

interact

First prompt should match

"Are you sure you want to continue connecting (yes/no/[fingerprint])? "

Second and Third:

"Username for 'https://changingDomain': "

"Password for 'https://changingDomain': "

Its only hanging on the last one. Locally it works, but on the Kubernetes pod it hangs. This is a PostSync Script embedded in an ArgoCD Project.

Edit:

Version 2: #!/usr/bin/env expect spawn -noecho /scripts/clone_repos.sh

expect -re {yes/no}    
send "yes\r"

expect -re {^Username}
send "gitea_admin\r"

expect -re {^Password}
send -- "superSecretPass\r"

interact

This is working a lot better now! Still hanging strangely on the last input, the script just finishes without providing the password. The problem might lie in the containered setup. Is there a way to have more verbosity with expect?


Solution

  • As hinted at https://unix.stackexchange.com/questions/693807/how-to-correctly-use-spawn-expect-send-for-git-push

    ..after you send the password, you don't wait for the push to complete: the expect script runs out of commands to run and exits too early, killing the git process. After any send, you should expect something. In this case, you're expecting the spawned command to end which is denoted with expect eof

    So, adding a set timeout -1 and an expect eof at the end did the trick for me, even in the a challenging Kubernetes-based GitOps environment.

    #!/usr/bin/env expect
    
    set PASSWORD [exec cat password]
    set USERNAME [exec cat username]
    
    spawn -noecho /scripts/clone_repos.sh
    
    expect "Are*"
    send "yes\r"
    
    expect "User*"
    send "$USERNAME\r"
    
    expect "Pass*"
    send -- "$PASSWORD\r"
    
    set timeout -1  ; # no timeout
    expect eof