Created
January 17, 2016 18:00
-
-
Save cgmartin/49cd0aefe836932cdc96 to your computer and use it in GitHub Desktop.
Bash SSL Certificate Expiration Check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TARGET="mysite.example.net"; | |
RECIPIENT="[email protected]"; | |
DAYS=7; | |
echo "checking if $TARGET expires in less than $DAYS days"; | |
expirationdate=$(date -d "$(: | openssl s_client -connect $TARGET:443 -servername $TARGET 2>/dev/null \ | |
| openssl x509 -text \ | |
| grep 'Not After' \ | |
|awk '{print $4,$5,$7}')" '+%s'); | |
in7days=$(($(date +%s) + (86400*$DAYS))); | |
if [ $in7days -gt $expirationdate ]; then | |
echo "KO - Certificate for $TARGET expires in less than $DAYS days, on $(date -d @$expirationdate '+%Y-%m-%d')" \ | |
| mail -s "Certificate expiration warning for $TARGET" $RECIPIENT ; | |
else | |
echo "OK - Certificate expires on $expirationdate"; | |
fi; |
- First of, day-month-year. (it used to be year-month-day, yuck)
You said year-month-day, yuck, it has to be the most dumbest thing thing in this script. Year-month-day is the standard in programming.
- First of, day-month-year. (it used to be year-month-day, yuck)
You said year-month-day, yuck, it has to be the most dumbest thing thing in this script. Year-month-day is the standard in programming.
Lol ๐ then you change it to what you like.
There's a couple reasons why i made that change.
- We - western people - read from left to right. The most important part of the date in this context is the day and potentially the month. Having that readable from left to right makes a lot of sense (to me!).
- Same geographic argument, different reason. dd-mm-yyyy is the standard here, i'm used to that and can't stand the many differences.
As an aside, if year-month-day is a standard (didn't know that and I've been a programmer for decades by now), then i'll happily ignore that. I'll quote you for my reason: "it has to be the most dumbest thing" ๐
Working great, thanks very much for the script!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing script! Just what i needed as base to build on :)
I changed it quite a bit to be an alternative to LetsEncrypt expiry mails. Here's my version:
Yes, i used AI to make this better :)
A couple things to note:
DOMAIN_LIST_FILE
is a one line per domain fileEXPIRY_DAYS
, a list of specific expiry days to handle. This is for the notification (or mail in the old version). I didn't want to maintain a temp file so i solved it by just having a fixed list of days where you can receive a notification.NOTIFICATION_SCRIPT
, the first argument is the title, second is the expiry message. In my case i'm using home assistant and thus have the "luxury" of using it's notification API to send me an actual notification.The script can probably be simplified a little but this works really nice!
Run this as a daily cronjob and you're all set for domain expiry notifications.
Here is the
notify.sh
script, change where needed.I had to change things on the links i found so i guess those resources have changed compared to how home assistant works now. Therefore not posting links :)