Skip to content

Instantly share code, notes, and snippets.

@opthakur
Forked from cgmartin/check-certs.sh
Created July 15, 2020 07:20
Show Gist options
  • Select an option

  • Save opthakur/3bc69471656c1792350c2d4a0585abd8 to your computer and use it in GitHub Desktop.

Select an option

Save opthakur/3bc69471656c1792350c2d4a0585abd8 to your computer and use it in GitHub Desktop.

Revisions

  1. @cgmartin cgmartin created this gist Jan 17, 2016.
    16 changes: 16 additions & 0 deletions check-certs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    #!/bin/bash
    TARGET="mysite.example.net";
    RECIPIENT="hostmaster@mysite.example.net";
    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;