-1

I'm writing a bash script to create self-signed certificate.

The script works well until I have to sign the certificate. Indeed, i can't get my expect command to match the received string.

Here is the string my terminal returns :

Sign the certificate? [y/n]:

And here are my commands :

/usr/bin/expect <<EOD
spawn sudo openssl ca -config CA/ca.cnf -policy policy_anything -out CA/newcerts/webserver.cert -infiles CA/certrequests/webserver.csr
expect {Enter pass phrase for ./CA/private/cakey.pem:} {send "${PEMpassword}\n"}
expect -re {Sign the certificate? [^:]*:} {send "y\n"}
expect -re {1 out of 1 certificate requests certified, commit? [^:]*:} {send "y\n"}
expect eof
EOD

I'm not very familiar with expect nor regex but "[^:]*" worked for me until now for similar strings.

I have unsuccessfully tried those :

[?*]
\*
?:
[y/n]
\[y/n\]
"[y/n]"
[\w/\w]
[w+]
[\w+]

Thanks in advance for your suggestions !

Kasket
  • 1
  • 2
  • Always escape special regex metacharacters (they differ when used inside or outside of bracket expressions). `?` is one of them in your patterns – Wiktor Stribiżew Feb 19 '20 at 20:35

1 Answers1

0

The problem may be that you begin the regex with

Sign the certificate? ...

but ? is a special pattern character that means 0 or 1 times, so in this case you are matching the letter e 0 or 1 times. It will do that match, but then your regex continues with a space, which does not match the real ? character in the input. To remove the special meaning from the ? precede it with a backslash, \?, or use . to match any character more loosely. Note, you have the same problem on the following line too.

meuh
  • 11,500
  • 2
  • 29
  • 45