最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Mar 31 at 22:43 Stephan Kristyn asked Mar 31 at 21:43 Stephan KristynStephan Kristyn 15.8k15 gold badges93 silver badges158 bronze badges 8
  • 1 ? needs to be escaped in regular expressions. – Barmar Commented Mar 31 at 21:54
  • 1 What does that have to do with it? ? has special meaning in regular expressions, it means the previous pattern is optional, or it's used to make non-greedy quantification. – Barmar Commented Mar 31 at 21:59
  • 1 Square brackets also need to be escaped, they're used for character sets. – Barmar Commented Mar 31 at 22:00
  • 1 And parentheses are used for grouping, so they also have to be escaped. I think you need to review a regexp tutorial. – Barmar Commented Mar 31 at 22:01
  • 1 Never mind. The expect pattern is a glob-style pattern unless you use expect -re. – Barmar Commented Mar 31 at 22:03
 |  Show 3 more comments

1 Answer 1

Reset to default 0

As hinted at https://unix.stackexchange/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
发布评论

评论列表(0)

  1. 暂无评论