从 bash 脚本中确定证书仍然有效的天数
Determine how many days a certificate is still valid from within bash script
我想在标准 Ubuntu 14.04 服务器上运行的 bash 脚本中检查网站证书的有效期。 openssl 可用。
我已经想通了,我可以使用 openssl 来获取目标日期
$ echo | openssl s_client -connect google.com:443 2>/dev/null|openssl x509 -noout -enddate
notAfter=Dec 22 16:37:00 2016 GMT
但是如何解析结果日期并减去当前日期?或者有更好的解决方案吗?
使用 GNU date
的 %j
获取年中的第几天和减法的算术扩展:
$ echo $(( $(date -d "$(cut -d= -f2 <(echo 'notAfter=Dec 22 16:37:00 2016 GMT'))" '+%j') - $(date '+%j')))
73
$(date -d "$(cut -d= -f2 <(echo 'notAfter=Dec 22 16:37:00 2016 GMT'))" '+%j')
从我们获得的日期开始获取一年中的第几天,将进程替换中的 echo
命令替换为 <(echo 'notAfter=Dec 22 16:37:00 2016 GMT')
16=]你最初使用的命令
$(date '+%j')
获取今天的日期作为一年中的第几天
$(())
用于减去整数
date1="Dec 22 16:37:00 2016 GMT" # Future date
date2=$(date) # Current date
diff=$(($(date -d "$date1" +%j)-$(date -d "$date2" +%j))) #Diff between two date, %j is to show day of the year.
echo $diff #Display difference
73
或仅在一行中:
echo $(($(date -d "Dec 22 16:37:00 2016 GMT" +%j)-$(date +%j)))
73
单行
echo "(" $(date -d "`openssl x509 -in /etc/letsencrypt/live/d-herrmann.de/cert.pem -text -noout | grep "Not After" | cut -c 25-`" +%s) - $(date -d "now" +%s) ")" / 86400 | bc
我想在标准 Ubuntu 14.04 服务器上运行的 bash 脚本中检查网站证书的有效期。 openssl 可用。
我已经想通了,我可以使用 openssl 来获取目标日期
$ echo | openssl s_client -connect google.com:443 2>/dev/null|openssl x509 -noout -enddate
notAfter=Dec 22 16:37:00 2016 GMT
但是如何解析结果日期并减去当前日期?或者有更好的解决方案吗?
使用 GNU date
的 %j
获取年中的第几天和减法的算术扩展:
$ echo $(( $(date -d "$(cut -d= -f2 <(echo 'notAfter=Dec 22 16:37:00 2016 GMT'))" '+%j') - $(date '+%j')))
73
$(date -d "$(cut -d= -f2 <(echo 'notAfter=Dec 22 16:37:00 2016 GMT'))" '+%j')
从我们获得的日期开始获取一年中的第几天,将进程替换中的echo
命令替换为<(echo 'notAfter=Dec 22 16:37:00 2016 GMT')
16=]你最初使用的命令$(date '+%j')
获取今天的日期作为一年中的第几天$(())
用于减去整数
date1="Dec 22 16:37:00 2016 GMT" # Future date
date2=$(date) # Current date
diff=$(($(date -d "$date1" +%j)-$(date -d "$date2" +%j))) #Diff between two date, %j is to show day of the year.
echo $diff #Display difference
73
或仅在一行中:
echo $(($(date -d "Dec 22 16:37:00 2016 GMT" +%j)-$(date +%j)))
73
单行
echo "(" $(date -d "`openssl x509 -in /etc/letsencrypt/live/d-herrmann.de/cert.pem -text -noout | grep "Not After" | cut -c 25-`" +%s) - $(date -d "now" +%s) ")" / 86400 | bc