remove-expired-certs.sh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/sh
  2. # Begin remove-expired-certs.sh
  3. #
  4. # Version 20120211
  5. # Make sure the date is parsed correctly on all systems
  6. mydate()
  7. {
  8. local y=$( echo $1 | cut -d" " -f4 )
  9. local M=$( echo $1 | cut -d" " -f1 )
  10. local d=$( echo $1 | cut -d" " -f2 )
  11. local m
  12. [ -z "${d}" ] && d="0"
  13. [ "${d}" -lt 10 ] && d="0${d}"
  14. case $M in
  15. Jan) m="01";;
  16. Feb) m="02";;
  17. Mar) m="03";;
  18. Apr) m="04";;
  19. May) m="05";;
  20. Jun) m="06";;
  21. Jul) m="07";;
  22. Aug) m="08";;
  23. Sep) m="09";;
  24. Oct) m="10";;
  25. Nov) m="11";;
  26. Dec) m="12";;
  27. esac
  28. certdate="${y}${m}${d}"
  29. }
  30. DIR="$1"
  31. [ -z "$DIR" ] && DIR=$(pwd)
  32. today=$(date +%Y%m%d)
  33. find ${DIR} -type f -a -iname "*.crt" -printf "%p\n" | while read cert; do
  34. notafter=$(/usr/bin/openssl x509 -enddate -in "${cert}" -noout)
  35. date=$( echo ${notafter} | sed 's/^notAfter=//' )
  36. mydate "$date"
  37. if [ ${certdate} -lt ${today} ]; then
  38. echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  39. echo "EXPIRED CERTIFICATE FOUND $certdate: \"$(basename ${cert})\""
  40. echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  41. rm -f "${cert}"
  42. fi
  43. done