Skip to content

Instantly share code, notes, and snippets.

@lgg
Created January 10, 2016 05:16
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save lgg/6fba83b7660f3ee8c90d to your computer and use it in GitHub Desktop.
Save lgg/6fba83b7660f3ee8c90d to your computer and use it in GitHub Desktop.
Let's encrypt auto authenticator runner for multiply domains
#!/bin/bash
#Vars
web_service='nginx'
config_path='/usr/local/letssl/'
le_path='/opt/letsencrypt'
exp_limit=20;
#Func
function check_ssl {
echo $1
if [ -f $1 ]; then
#find domain
domain=`grep "^\s*domains" $1 | sed "s/^\s*domains\s*=\s*//" | sed 's/(\s*)\|,.*$//'`
cert_file="/etc/letsencrypt/live/$domain/fullchain.pem"
#check if cert exist
if [ ! -f $cert_file ]; then
echo "[ERROR] certificate file not found for domain $domain."
fi
#get exp date
exp=$(date -d "`openssl x509 -in $cert_file -text -noout|grep "Not After"|cut -c 25-`" +%s)
datenow=$(date -d "now" +%s)
days_exp=$(echo \( $exp - $datenow \) / 86400 |bc)
echo "Checking expiration date for $domain..."
#check exp date
if [ "$days_exp" -gt "$exp_limit" ] ; then
echo "The certificate is up to date, no need for renewal ($days_exp days left)."
else
echo "The certificate for $domain is about to expire soon. Starting webroot renewal script..."
#update cert
$le_path/letsencrypt-auto certonly -a webroot --agree-tos --renew-by-default --config $1
#reload webserver
echo "Reloading $web_service"
/usr/sbin/service $web_service reload
#done
echo "Renewal process finished for domain $domain"
fi
else
echo "[ERROR] config file does not exist: $1"
fi
}
#loop
configs="$config_path*.ini"
for f in $configs
do
#echo $f
check_ssl $f
done
exit 0;
@lgg
Copy link
Author

lgg commented Jun 7, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment