I am trying to auto generate SSL certs and upload them to my shared hosing on namecheap.com
The host does not offer any way to auto manage ssl certs without paying a lot of money.
I am trying to get this script working
https://catelin.net/2018/03/24/fully-automate-ssl-tls-certificate-renewal-with-cpanel/
The idea is to run bash scrips on your own linux box that will update the ssl certs through cpanel.
I am having difficulty with this section of code. In my actual code I have updated the server name info. The main problem is that I have very little experience writing in bash script.
certificate=$(echo |openssl s_client -servername yourserver.com -connect yourserver.com:443 2>/tmp/cert.tmp|openssl x509 -checkend $[86400 * $RENEW] -enddate)
if [ "$certificate" == "" ]; then
echo "Error: unable to check certificate"
else
if [[ $certificate =~ (.*)Certificate will expire ]]; then
echo $certificate
...
I am getting an error here (my first of many errors I am sure...)
./certupdate.sh: line 19: syntax error in conditional expression
./certupdate.sh: line 19: syntax error near `will'
./certupdate.sh: line 19: ` if [[ $certificate =~ (.*)Certificate will expire ]]; then'
Any help would be great.
Or, if someone has a better idea on how to update the ssl certs that would be even better. Something in all PHP would be great as I am more familiar with that.
The shell parses each line into tokens by splitting on whitespace. The syntax of the
[[
built-in with=~
requires one token on each side. You can prevent splitting on whitespace by putting backslashes in front of every whitespace character which is not a token separator, or quoting the sequence which should be a single token.That aside, you really don't need a regular expression here. (And if you do use one, the parentheses around
.*
are superfluous. In fact the whole.*
is superfluous.)That aside, the script you are trying to copy has a number of other issues, albeit more minor ones. Probably just try to find a better blog to copy/paste from.
Here's a quick refactoring to hopefully make the script more idiomatic, but I might have missed some issues, and don't have any way to test this.
The
%c
format specifier fordate
includes the year, where the original code omitted it. I consider this change a feature rather than a bug.There are still a lot of hard-coded paths etc which should probably be parametrized better.
The stderr output from
openssl
is modest enough that I don't think we absolutely need to discard it; on the other hand, dumping it in a temporary file will almost certainly hide useful diagnostics when something actually goes wrong (network down or whatever).