Skip to content

Instantly share code, notes, and snippets.

@askin
Last active October 3, 2019 08:32
Show Gist options
  • Save askin/d5efb7f37b61520a4fc87dd0dcd132f9 to your computer and use it in GitHub Desktop.
Save askin/d5efb7f37b61520a4fc87dd0dcd132f9 to your computer and use it in GitHub Desktop.

Revisions

  1. askin revised this gist Oct 3, 2019. No changes.
  2. askin created this gist Oct 2, 2019.
    42 changes: 42 additions & 0 deletions sslcheck-expiry.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/bin/bash
    # Simple SSL cert days-till-expiry check script
    # by Askin Ozgur https://blog.yollu.com [email protected]
    # Derived from Glen Scott, www.glenscott.net

    PORT=443
    if [ ${#} == 2 ]; then
    DOMAIN=$1
    PORT=$2
    elif [ ${#} == 1 ]; then
    DOMAIN=$1
    else
    echo "Usage: $0 example.tld [port]"
    exit 1
    fi

    # Check port is valid?
    if ! [[ ${PORT} == +([0-9]) ]]; then
    echo "Port must be numeric!!!"
    exit 1
    fi

    openssl_output=$(echo "
    GET / HTTP/1.0
    EOT" \
    | openssl s_client -connect ${DOMAIN}:${PORT} -servername ${DOMAIN} 2>&1);

    if [[ "$openssl_output" = *"-----BEGIN CERTIFICATE-----"* ]]; then

    cert_expiry_date=$(echo "$openssl_output" \
    | sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p' \
    | openssl x509 -enddate \
    | awk -F= ' /notAfter/ { printf("%s\n",$NF); } ');

    seconds_until_expiry=$(echo "$(date --date="$cert_expiry_date" +%s) - $(date +%s)" |bc);
    days_until_expiry=$(echo "$seconds_until_expiry/(60*60*24)" |bc);

    echo "$days_until_expiry";
    else
    echo "NOT_FOUND";
    fi
    exit 1