11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SSL証明書の有効期限を確認するスクリプト

Last updated at Posted at 2015-02-02

openssl コマンドを使うと証明書の情報が取得できるので、証明書の有効期限のチェックを行うスクリプトを書きました。

#!/bin/bash

## validate_certs - Check SSL certificate expiration date

WARN_DAYS=30
TARGETS=(
    "secure.example.com"
)

function show_enddate()
{
    echo "" | openssl s_client -connect $1:443 2> /dev/null | openssl x509 -enddate -noout | sed 's/notAfter\=//'
}

echo "SSL Certificate Expiration Checking ($(date -R))"
echo

i_now=$(date "+%Y%m%d%H%M%S")
i_warn=$(date -d "$WARN_DAYS days" "+%Y%m%d%H%M%S")
for host in ${TARGETS[@]}; do
    enddate=$(LANG=C date -d"$(show_enddate $host)")
    i_enddate=$(date -d"$enddate" "+%Y%m%d%H%M%S")
    if [ $i_now -ge $i_enddate ]; then
        echo "[SEVERE]  \`$host' already expired on $enddate"
    elif [ $i_warn -ge $i_enddate ]; then
        echo "[WARN] \`$host' will expire on $enddate"
    fi
done

echo
echo "EOF"

変数 TARGETS に監視するドメイン名を列挙しておきます。
有効期限までの日数が変数 WARN_DAYS よりも小さい場合、画面にそのドメインを [WARN] というメッセージとともに出力します。ちなみに有効期限を過ぎてしまっているドメイン名は [SEVERE] というメッセージになります。

このスクリプトの出力をメールで送信するように cron に登録しておくと証明書の更新忘れがなくなる、かも。

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?