用于计算网站中 SSL 证书到期剩余天数的 Bash 脚本

sol*_*ier 1 command-line bash scripts ssl

我有一个名为的网站xplosa.com,它有一个有效的 SSL 证书,我通过 bash 脚本应该能够计算剩余天数到期。我知道有很多替代方法可以完成这项工作,但我喜欢使用 Ubuntu bash 。顺便说一句,我正在使用Ubuntu 18.04

这是我的示例逻辑

#!/bin/bash

get_the_cert_expiry_date() {
    # command to retrieve the expiry date
}

currentDate="$(date +%Y-%m-%d)"
website="xplosa.com"
certExpDate="$(get_the_cert_expiry_date)"
count=$((currentDate - certExpDate))

echo "remaining days for expiry: ${count}"
Run Code Online (Sandbox Code Playgroud)
  • 这是一个正确的逻辑吗?
  • 如何实施 get_the_cert_expiry_date

Akh*_*hil 6

这应该工作

#!/bin/bash

website="xplosa.com"
certificate_file=$(mktemp)
echo -n | openssl s_client -servername "$website" -connect "$website":443 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $certificate_file
date=$(openssl x509 -in $certificate_file -enddate -noout | sed "s/.*=\(.*\)/\1/")
date_s=$(date -d "${date}" +%s)
now_s=$(date -d now +%s)
date_diff=$(( (date_s - now_s) / 86400 ))
echo "$website will expire in $date_diff days"
rm "$certificate_file"
Run Code Online (Sandbox Code Playgroud)