在解析输出之前评估 OpenSSL 的输出
Evaluate output of OpenSSL before parsing output
使用以下代码检查 SSL 证书是否过期:
cat localdomains | xargs -L 1 bash -c 'openssl s_client -connect [=10=]:443 -servername [=10=] 2> /dev/null | openssl x509 -noout -enddate | cut -d = -f 2 | xargs -I {} echo {} [=10=]'
当我 运行 进入没有返回证书的域时,我正在努力思考如何将输出更改为 N/A 之类的内容,而不是尝试评估 "cut -d = -f 2" 然后 xargs -I
如果您在变量中捕获命令的输出,则可以对其进行验证。假设这不一定是一个班轮:
#!/bin/bash
while read domain; do
expiry=$(openssl s_client -connect ${domain}:443 -servername ${domain} 2>/dev/null </dev/null | \
openssl x509 -noout -enddate 2>&1 | cut -d = -f 2)
# validate output with date
if date -d "${expiry}" > /dev/null 2>/dev/null ; then
echo ${expiry} ${domain}
else
echo "N/A" ${domain}
fi
done
注意几件事:
- 将 /dev/null 重定向到
openssl s_client
的标准输入以获取提示(see here for technical details)
- 将第二个
openssl x509
的 stderr 重定向到 stdout 以便对其进行验证。
- 您可以使用
grep
或 sed
来验证输出。我发现 date
很方便(如果你想用它来重新格式化日期,那就更方便了)。
我通过将它放入文件 check_cert_expiry.sh
:
来测试这个解决方案
$ cat localdomains
whosebug.com
example.example
google.com
$ cat localdomains | ./check_cert_expiry.sh
Aug 14 12:00:00 2019 GMT whosebug.com
N/A example.example
Feb 21 09:37:00 2018 GMT google.com
干杯!
使用以下代码检查 SSL 证书是否过期:
cat localdomains | xargs -L 1 bash -c 'openssl s_client -connect [=10=]:443 -servername [=10=] 2> /dev/null | openssl x509 -noout -enddate | cut -d = -f 2 | xargs -I {} echo {} [=10=]'
当我 运行 进入没有返回证书的域时,我正在努力思考如何将输出更改为 N/A 之类的内容,而不是尝试评估 "cut -d = -f 2" 然后 xargs -I
如果您在变量中捕获命令的输出,则可以对其进行验证。假设这不一定是一个班轮:
#!/bin/bash
while read domain; do
expiry=$(openssl s_client -connect ${domain}:443 -servername ${domain} 2>/dev/null </dev/null | \
openssl x509 -noout -enddate 2>&1 | cut -d = -f 2)
# validate output with date
if date -d "${expiry}" > /dev/null 2>/dev/null ; then
echo ${expiry} ${domain}
else
echo "N/A" ${domain}
fi
done
注意几件事:
- 将 /dev/null 重定向到
openssl s_client
的标准输入以获取提示(see here for technical details) - 将第二个
openssl x509
的 stderr 重定向到 stdout 以便对其进行验证。 - 您可以使用
grep
或sed
来验证输出。我发现date
很方便(如果你想用它来重新格式化日期,那就更方便了)。
我通过将它放入文件 check_cert_expiry.sh
:
$ cat localdomains
whosebug.com
example.example
google.com
$ cat localdomains | ./check_cert_expiry.sh
Aug 14 12:00:00 2019 GMT whosebug.com
N/A example.example
Feb 21 09:37:00 2018 GMT google.com
干杯!