使用到期日期过滤器在任何 centos 目录中查找所有证书文件

find all certificate files in any centos directory using the expiry date filter

拜托,我需要在centos box的任意目录下找到所有的证书文件。

我尝试使用“exec”命令和 grep“not after”来“查找”。这仅显示证书的到期日期,但我也需要找到实际文件:

find /etc/  -type f -exec openssl x509 -in {} -noout -text \; |
grep -i  "not after"

什么命令也可以列出证书文件及其到期日期的内容?

你可以使用这个find + awk:

while IFS= read -rd '' cert; do
   printf '%s :: ' "$cert"
   openssl x509 -in "$cert" -noout -text |
   awk -F ' *Not After : ' 'NF == 2 {print ; exit}'
done < <(find /etc -type f -print0)

带有辅助脚本的版本:

cat /root/expiry.sh

#!/bin/bash
name=
expiry=$(openssl x509 -in $name -noout -text 2>/dev/null | grep -i "not after")
if [[  $PIPESTATUS -eq 0 ]]; then
    echo -e "${name}\t${expiry}"
fi

这样执行:

find /etc/  -type f -exec /root/expiry.sh "{}" \;
/etc/ssl/certs/ssl-cert-snakeoil.pem                Not After : Mar 30 22:59:59 2027 GMT
/etc/ssl/certs/ca-certificates.crt              Not After : Dec 31 09:37:37 2030 GMT