如果我在Mac或Linux中有实际的文件和Bash shell,我如何查询证书文件何时到期?不是网站,实际上是证书文件本身,假设我有csr、key、pem和chain文件。
当前回答
openssl:
openssl x509 -enddate -noout -in file.pem
输出在表单上:
notAfter=Nov 3 22:23:50 2014 GMT
还可以参阅MikeW的回答,了解如何轻松地检查证书是否过期,或者是否会在某个时间段内过期,而不必解析上面的日期。
其他回答
一行检查true/false,如果域名证书将在一段时间后过期(例如。15天):
openssl x509 -checkend $(( 24*3600*15 )) -noout -in <(openssl s_client -showcerts -connect my.domain.com:443 </dev/null 2>/dev/null | openssl x509 -outform PEM)
if [ $? -eq 0 ]; then
echo 'good'
else
echo 'bad'
fi
如果(出于某种原因)您想在Linux中使用GUI应用程序,请使用gcr-viewer(在大多数发行版中,它是由包gcr安装的(否则在包gcr-viewer中))
gcr-viewer file.pem
# or
gcr-viewer file.crt
我已经做了一个与此相关的bash脚本来检查证书是否过期。如果需要,您可以使用相同的方法。
脚本
https://github.com/zeeshanjamal16/usefulScripts/blob/master/sslCertificateExpireCheck.sh
自述
https://github.com/zeeshanjamal16/usefulScripts/blob/master/README.md
这里有一个bash函数,它检查所有服务器,假设您使用的是DNS循环。注意,这需要GNU日期,不能在Mac OS上工作
function check_certs () {
if [ -z "$1" ]
then
echo "domain name missing"
exit 1
fi
name="$1"
shift
now_epoch=$( date +%s )
dig +noall +answer $name | while read _ _ _ _ ip;
do
echo -n "$ip:"
expiry_date=$( echo | openssl s_client -showcerts -servername $name -connect $ip:443 2>/dev/null | openssl x509 -inform pem -noout -enddate | cut -d "=" -f 2 )
echo -n " $expiry_date";
expiry_epoch=$( date -d "$expiry_date" +%s )
expiry_days="$(( ($expiry_epoch - $now_epoch) / (3600 * 24) ))"
echo " $expiry_days days"
done
}
输出的例子:
$ check_certs stackoverflow.com
151.101.1.69: Aug 14 12:00:00 2019 GMT 603 days
151.101.65.69: Aug 14 12:00:00 2019 GMT 603 days
151.101.129.69: Aug 14 12:00:00 2019 GMT 603 days
151.101.193.69: Aug 14 12:00:00 2019 GMT 603 days
对于MAC OSX (El Capitan),我对Nicholas的例子进行了修改。
for pem in /path/to/certs/*.pem; do
printf '%s: %s\n' \
"$(date -jf "%b %e %H:%M:%S %Y %Z" "$(openssl x509 -enddate -noout -in "$pem"|cut -d= -f 2)" +"%Y-%m-%d")" \
"$pem";
done | sort
样例输出:
2014-12-19: /path/to/certs/MDM_Certificate.pem
2015-11-13: /path/to/certs/MDM_AirWatch_Certificate.pem
macOS不喜欢我系统上的——date=或——iso-8601标志。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 安装tzdata非交互式
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 在Cygwin中对HTTPS URL运行wget时,如何修复证书错误?
- 在Bash中检查变量是否存在于列表中
- 查看PS命令的全部输出
- 确保一次只运行一个shell脚本实例的快速方法
- 如何在Windows命令提示符下运行.sh ?
- 如何从命令行将每两行合并为一行?
- 如何复制在bash所有目录和文件递归?
- Linux命令将域名转换为IP
- 如何从命令行在windows中找到mysql数据目录
- 在Bash命令提示符上添加git分支
- 匹配前后的Grep字符?
- 带有多个条件的Bash if语句将抛出错误