我想获得证书到期前的天数。使用 openssl 可以轻松提取日期
> cat cert | openssl x509 -noout -enddate
notAfter=Jun 8 17:07:09 2021 GMT
不幸的是,解析日期 Jun 8 17:07:09 2021 GMT
并找到到今天为止的日子并不是那么简单。目标是拥有
> cat cert | openssl x509 -noout -enddate | ...some commands...
15
意思是证书到期前 15 天。
我知道 openssl -checkend
选项,但这只是一个 bool 值,我想要天数。
最佳答案
您可以使用这个 liner shell 脚本:
expiryDays=$(( ($(date -d "$(openssl x509 -in cert -enddate -noout | cut -d= -f2)" '+%s') - $(date '+%s')) / 86400 ))
这是分手:
openssl ...
命令以 notAfter=...
格式打印到期日期cut -d= -f2
获取 =
之后的文本date -d ...
'+%s'`: 获取到期日的 EPOCH 秒值date '+%s'
:获取今天日期的 EPOCH 秒值(epochExpiry - epochToday)/86400
:获取 2 个 EPOCH 值的天数之差https://stackoverflow.com/questions/66920940/