LoginSignup
8
6

More than 5 years have passed since last update.

SSL証明書の有効期限を確認するbash script

Last updated at Posted at 2014-05-22

SSL証明書の期限を確認する

こんな感じで確認できる。

$ sslexpire secure.nicovideo.jp
https://secure.nicovideo.jp SSL certificate expires after 39 days and 13 hours (2014/06/30 23:59:00 JST)
#!/bin/bash
#
# usage: $0 domain[:port] [alert days] [timeout]
# ex)
# $0 yahoo.com 365
#

server=${1//:*/}
port=${1/*:/}
test "$server" = "$port" && port=443
limit=${2-60}   # default 60 days
timeout=${3-3}  # default timeout 3 seconds

notafter=$(timeout $timeout openssl s_client -connect $server:$port < /dev/null 2> /dev/null | openssl x509 -enddate -noout 2> /dev/null | cut -d= -f2)

if [ "$notafter" ]; then
    s=$(( $(date --date "$notafter" +%s) - $(date +%s) ))

    if [ $limit -ge $((s / 86400)) ]; then
        d=$(date --date "$notafter" +"%Y/%m/%d %H:%M:%S %Z")
        days=$((s/86400))
        hours=$((s%86400/3600))
        echo -n "https://$server SSL certificate expires after "
        if [ $days -gt 0 ]; then
            echo -n $days day
            if [ $days -gt 1 ]; then
                echo -n s
            fi
        fi
        if [ $hours -gt 0 ]; then
            if [ $days -gt 0 ]; then
                echo -n " and "
            fi
            echo -n $hours hour
            if [ $hours -gt 1 ]; then
                echo -n s
            fi
        fi
        echo " ($d)"

        exit 1
    fi
else
    echo "https://$server is not supported SSL or timeout?"
    exit 1
fi

タイムアウトの処理はもうちょっとなんとかしたほうがいい。

8
6
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
8
6