인증서 만료일 확인

Let’s Encrypt에서 인증서 연장 실패 메일을 더 이상 보내지 않겠다고 한다.

그래서 코파일럿의 도움으로 인증서 만료일까지 몇 일 남아 있는지 확인할 수 있는 스크립트를 작성했다.

#!/bin/bash
cert_files=$(sudo find /etc/letsencrypt/live -name "fullchain.pem")
IFS=$'\n' read -r -d '' -a cert_array <<< "$cert_files"
today=$(date -u +%s)

for cert in "${cert_array[@]}"; do
  domain=$(echo "${cert}" | sed -e 's|/etc/letsencrypt/live/\([^/]*\)/fullchain.pem|\1|')
  expiry_date=$(sudo openssl x509 -enddate -noout -in "$cert" | sed -e 's/^notAfter=//')
  expiry=$(date -ud "${expiry_date}" +%s)
  remaining_days=$(( (expiry - today) / 86400 ))
  echo ${domain}: ${remaining_days}
done