解析日期以计算直到Shell脚本中的证书到期为止的天数 [英] parsing the date to calculate days until cert expiry from a shell script
本文介绍了解析日期以计算直到Shell脚本中的证书到期为止的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想证明证书过期的日子.使用openssl提取日期很容易
I would like to get the days until a cert expires. Extracting the dates is easy with openssl
> cat cert | openssl x509 -noout -enddate
notAfter=Jun 8 17:07:09 2021 GMT
不幸的是,解析日期 Jun 8 17:07:09 2021 GMT
并找到直到今天的日子并不是那么简单.目标是拥有
Unfortunately parsing the date Jun 8 17:07:09 2021 GMT
and finding the days until today is not so straight forward. The goal would be to have
> cat cert | openssl x509 -noout -enddate | ...some commands...
15
这是指证书过期之前的15天.
Meaning 15 days until the cert expires.
我知道openssl -checkend
选项,但这只是一个布尔值,我需要天数.
I am aware of the openssl -checkend
option, but that is just a boolean and I want the number of days.
推荐答案
您可以使用以下一个线性脚本:
You may use this one liner shell script:
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 秒值-
日期'+%s'
:获取今天日期的EPOCH秒值 -
(epochExpiry-epochToday)/86400
:获取2个EPOCH值之差,以天数为单位
openssl ...
command to print expiry date innotAfter=...
formatcut -d= -f2
to get text after=
date -d ...
'+%s'`: to get EPOCH seconds value for expiry datedate '+%s'
: to get EPOCH seconds value for today's date(epochExpiry - epochToday) / 86400
: to get difference of 2 EPOCH values as number of days
这篇关于解析日期以计算直到Shell脚本中的证书到期为止的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文