Skip to content

Instantly share code, notes, and snippets.

@mcchots
Forked from damkh/removeExpiredCerts.sh
Last active September 12, 2023 10:26
Show Gist options
  • Save mcchots/9043caf85283f327021e7dd6763e13e5 to your computer and use it in GitHub Desktop.
Save mcchots/9043caf85283f327021e7dd6763e13e5 to your computer and use it in GitHub Desktop.
Remove expired certificates from a keystore
#!/bin/bash
# remove expired certs from a keystore
# set FN to the keystore file
KEYSTORE=$1
KEYSTOREPASS=$2
echo "finding expired certs..."
ALIASES=`keytool -list -v -keystore $KEYSTORE -storepass $(echo $KEYSTOREPASS) | grep -i 'alias\|until' `
echo "$ALIASES" > aliases.txt
i=1
# Split dates and aliases to different arrays
while read p; do
if ! ((i % 2)); then
arr_date+=("$p")
else
arr_cn+=("$p")
fi
i=$((i+1))
done < aliases.txt
i=0
# Parse until-dates ->
# convert until-dates to "seconds from 01-01-1970"-format ->
# compare until-dates with today-date ->
# delete expired aliases
for date_idx in $(seq 0 $((${#arr_date[*]}-1)));
do
a_date=`echo ${arr_date[$date_idx]} | awk -F"until: " '{print $2}'`
if [ `date +%s --date="$a_date"` -lt `date +%s` ];
then
echo "removing ${arr_cn[$i]} expired: $a_date"
alias_name=`echo "${arr_cn[$i]}" | awk -F"name: " '{print $2}'`
keytool -delete -alias "$alias_name" -keystore $KEYSTORE -storepass $(echo $KEYSTOREPASS)
fi
i=$((i+1))
done
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment