Last active
August 11, 2021 10:13
-
-
Save cherts/fe31686ea0f9d8f9dcfbacf336203663 to your computer and use it in GitHub Desktop.
Check SSL certificate expires days
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
| #!/usr/bin/env bash | |
| # | |
| # Program: Check SSL certificate expires days <ssl_cert_expire.sh> | |
| # | |
| # Author: Mikhail Grigorev <sleuthhound at gmail dot com> | |
| # | |
| # Current Version: 1.0.2 | |
| # | |
| # License: | |
| # This program is distributed in the hope that it will be useful, | |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| # | |
| DEBUG=0 | |
| SOURCE="${BASH_SOURCE[0]}" | |
| while [ -h "$SOURCE" ]; do | |
| DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
| SOURCE="$(readlink "$SOURCE")" | |
| [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" | |
| done | |
| SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" | |
| SCRIPT_NAME=$(basename "$0") | |
| if [ ${DEBUG} -gt 0 ]; then | |
| exec 2>> /var/log/${SCRIPT_NAME%.*}.log | |
| set -x | |
| fi | |
| _usage() { | |
| echo "" | |
| echo "usage: $0 [-i|-d] HOST PORT SNI" | |
| echo " -i Show Issuer" | |
| echo " -d Show valid days remaining" | |
| echo "" | |
| exit 1 | |
| } | |
| [ $# -lt 2 ] && _usage | |
| OPT=$1 | |
| HOST=$2 | |
| PORT=$3 | |
| SNI=$4 | |
| PROTO=$5 | |
| if [ -z "${SNI}" ]; then | |
| SERVER_NAME=${HOST} | |
| else | |
| SERVER_NAME=${SNI} | |
| fi | |
| if [ -z "${PORT}" ]; then | |
| PORT=443 | |
| fi | |
| if [ -n "${PROTO}" ]; then | |
| START_TLS="-starttls ${PROTO}" | |
| fi | |
| case ${OPT} in | |
| -d) | |
| END_DATE=$(echo -e "quit\n" | openssl s_client -servername ${SERVER_NAME} -host ${HOST} -port ${PORT} -showcerts ${START_TLS} -prexit 2>&1 | sed -n '/BEGIN CERTIFICATE/,/END CERT/p' | openssl x509 -text 2>/dev/null | sed -n 's/ *Not After : *//p') | |
| if [ -n "${END_DATE}" ]; then | |
| END_DATE_SECOND=$(date '+%s' --date "${END_DATE}") | |
| NOW_SECONDS=$(date '+%s') | |
| END_DAY=$(echo "(${END_DATE_SECOND}-${NOW_SECONDS})/86400" | bc) | |
| echo -n "${END_DAY}" | |
| else | |
| echo -n "0" | |
| fi | |
| ;; | |
| -i) | |
| ISSUE_DN=$(echo -e "quit\n" | openssl s_client -servername ${SERVER_NAME} -host ${HOST} -port ${PORT} -showcerts ${START_TLS} -prexit 2>&1 | sed -n '/BEGIN CERTIFICATE/,/END CERT/p' | openssl x509 -text 2>/dev/null | sed -n 's/ *Issuer: *//p') | |
| if [ -n "${ISSUE_DN}" ]; then | |
| ISSUER=$(echo -n "${ISSUE_DN}" | sed -n 's/.*CN=*//p' | sed 's/^[ \t]*//;s/[ ]*$//' | sed 's/^[= ]*//') | |
| echo -n "${ISSUER}" | |
| else | |
| echo -n "Not found" | |
| fi | |
| ;; | |
| *) | |
| _usage | |
| ;; | |
| esac | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment