Skip to content

Instantly share code, notes, and snippets.

@dkrusky
Last active March 11, 2016 12:45
Show Gist options
  • Save dkrusky/30edba0cc59f7c5741bf to your computer and use it in GitHub Desktop.
Save dkrusky/30edba0cc59f7c5741bf to your computer and use it in GitHub Desktop.
Install or update letsencrypt and generate a certificate for a cPanel user and domain, and email it to the user.
#!/bin/sh
INSTRUCTIONS="
<html>
<head>
</head>
<body>
<h3>Installation Instructions</h3>
<b>Step 1</b>
<p>Login to your cPanel account and look for the following icon and click it</p>
<img src=\"https://i.imgur.com/IEay2hm.png\" />
<br><br>
<b>Step 2</b>
<p>Locate the following link, and click on it</p>
<img src=\"https://i.imgur.com/9kPCEYB.png\" />
<br><br>
<b>Step 3</b>
<p>Using the attached files, paste the contents of each file into the boxes as shown below, then click the install button at the bottom</p>
<img src=\"https://i.imgur.com/0Rb0wRB.png\" />
</body>
</html>
"
while true; do
read -p "Enter the cPanel username [or x to exit] : " username
case $username in
[Xx] ) exit;;
* )
if [ ! -d /home/$username/public_html ]; then
echo "cPanel username '$username' does not exist. Try again."
else
break
fi
;;
esac
done
while true; do
read -p "Enter the domain [or x to exit] : " domain
case $domain in
[Xx] ) exit;;
* )
if [ -z "$(echo $domain | grep -P '(?=^.{5,254}$)(^(?:(?!\d+\.)[a-zA-Z0-9_\-]{1,63}\.?)+(?:[a-zA-Z]{2,})$)')" ]; then
echo "The domain '$domain' is not a valid format. Try again."
else
if [ ! -d /home/$username/mail/$domain ]; then
echo "The domain '$domain' does not exist in the user account '$username'. Please try again"
else
break
fi
fi
;;
esac
done
if[ -d /home/$username/public_html ]; then
cd ~
if[ ! -d ~/letsencrypt ]; then
git clone https://github.com/letsencrypt/letsencrypt
fi
fi
cd letsencrypt
git pull origin master
./letsencrypt-auto --text --agree-tos --email abuse@$domain certonly --renew-by-default --webroot --webroot-path /home/$username/public_html/ -d $domain -d www.$domain
# Send email notice to recipient
UUIDMSG="$(uuidgen)/$(hostname)"
(
echo "From: root@$(hostname)
To: abuse@$domain
Subject: Your SSL certificate for $domain is ready
Content-Type: multipart/mixed; boundary=\"$UUIDMSG\"
MIME-Version: 1.0
--$UUIDMSG
Content-Type: text/html
Content-Disposition: inline
$INSTRUCTIONS
--$UUIDMSG
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream; name=cert.pem
Content-Disposition: attachment; filename=cert.pem
$(base64 /etc/letsencrypt/live/$domain/cert.pem)
--$UUIDMSG
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream; name=privkey.pem
Content-Disposition: attachment; filename=privkey.pem
$(base64 /etc/letsencrypt/live/$domain/privkey.pem)
--$UUIDMSG--"
) | sendmail -t
# TODO - Install certificate generated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment