Last active
August 29, 2015 14:20
-
-
Save robinkunde/a9690d515e6a1ad98da3 to your computer and use it in GitHub Desktop.
Bash script that checks expiration date of SSL certificates
This file contains 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 | |
DATE_STRING="1 months" | |
display_usage() { | |
echo "This script displays the expiration dates of the passed SSL certificates if they expire before a set date (defaults to one month from now)." | |
echo "See usage of the -d parameter of the date command on how to format the optional date string." | |
echo -e "\nUsage:\n$0 [-d \"DATE_STRING\"] FILE... \n" | |
} | |
if [[ $# < 1 || $1 == "--help" || $1 == "-h" ]] | |
then | |
display_usage | |
exit 0 | |
fi | |
while [[ $# > 1 ]] | |
do | |
key="$1" | |
case $key in | |
-d) | |
DATE_STRING="$2" | |
shift | |
;; | |
-*) | |
echo "Unknown parameter: $1" | |
display_usage | |
exit 1 | |
;; | |
*) | |
break | |
;; | |
esac | |
shift | |
done | |
WARN_TIMESTAMP=$(date -d "$DATE_STRING" +%s) | |
EXIT_CODE=0 | |
while [[ $# > 0 ]] | |
do | |
if [ ! -f "$1" ] | |
then | |
EXIT_CODE=1 | |
echo "$1 does not exists" | |
shift | |
continue | |
fi | |
EXPIRATION_DATE=$(openssl x509 -enddate -noout -in $1 | sed 's/notAfter=//') | |
EXPIRATION_TIMESTAMP=$(date -d "$EXPIRATION_DATE" +%s) | |
if [[ $EXPIRATION_TIMESTAMP < $WARN_TIMESTAMP ]] | |
then | |
echo "$1 expires on $EXPIRATION_DATE" | |
fi | |
shift | |
done | |
exit $EXIT_CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment