【问题标题】:Openssl output hanging when getting -enddate获取-enddate时Openssl输出挂起
【发布时间】:2016-03-04 15:17:27
【问题描述】:

我一直在编写一个脚本来获取一些证书详细信息,而不是我的格式正在被熨平,当我尝试解析 EndDate="openssl s_client -connect $host:$port 2>/dev/null | openssl x509 -enddate -noout | grep "Not After" | awk '{print $4, $5, $7}'" 时,脚本现在挂起。

这是我目前正在编写的完整脚本以供参考,因为我刚刚掌握了脚本编写的窍门,所以其中大部分都被破解了。

# User input for the host or url of the certificate to check 
echo "What host IP or URL certificate would you like to check: "
    read host
# User input for the port number of the certificate to check
echo "What is the port number for the host's IP or URL: "
    read port
# Input Verification post
echo "Host connection information = $host:$port"
# openssl expiration date checks for the week
echo "::Certificate expiration date::"
EndDate=`openssl s_client -connect $host:$port 2>/dev/null | openssl x509 -   enddate -noout | grep "Not After" | awk '{print $4, $5, $7}'`
DatePlus7=`date -ud "+7 day" | awk '{print $2, $3, $6}'`
if [ "$EndDate" = "$DatePlus7"]
then
        echo "Certificate has expired or will do so within 7 days!"
        echo "(or is invalid/not found)"
else
        echo "Certificate is good for another week!"
fi

最终我希望能够为管理员输出echo | openssl s_client -connect $host:$port 2>/dev/null | openssl x509 -enddate -noout,这出现在fi 语句之后。谁能帮我解决这个问题?

【问题讨论】:

  • 过早地将 std-err 输出重定向到 /dev/null 通常是错误的。你知道它在哪里失败了吗?您是否尝试仅执行 openssl s_client -connect $host:$port 并且如果那是干净的,则添加下一段,直到您看到问题。祝你好运。
  • 如果您在脚本“挂起”时点击ctrl-d,它会恢复并继续运行吗?您可能只需要强制s_client 终止连接而不是等待输入。
  • 所以我让脚本运行了相当长的时间,大约 10-12 分钟,如果我使用我的原版脚本不理会它就会完成。我也可以ctrl-d 并且它确实恢复了。如果我也只是使用适当的变量执行openssl s_client -connect $host:$port,它运行良好没有问题。 @shellter 我不确定它在管道中的哪个位置失败,因为我刚刚开始使用 bash。

标签: linux bash shell ssl openssl


【解决方案1】:

从小处着手构建比从大处着手并向下调试更容易。

这里有一个更简单的方法来重现你的问题,它也只是挂起:

openssl s_client -connect google.com:443

既然问题如此简单和狭隘,谷歌搜索“为什么 openssl s_client 会挂起?”导致useful information推荐echo -n | ...“给服务器一个响应,以便释放连接”。这应该足以更进一步(还有其他问题)。

无论如何,这里有一个更短的方法:

if openssl s_client -connect google.com:443 2> /dev/null < /dev/null |
    openssl x509 -checkend $((60*60*24*7)) -noout -in /dev/stdin
then
  echo "The certificate is good."
else
  echo "The certificate expires within a week."
fi

【讨论】:

  • 所以这太棒了,真的让我的事情降低了一个档次,尽管我仍然对2&gt; /dev/null &lt; /dev/null | 位以及为什么需要-in /dev/stdin 感到困惑,但我自己对这些问题进行了更多研究。这是一个很大的帮助,所以谢谢,现在我只需要添加日期和它的金色!
【解决方案2】:

这将打印到期日期而不会挂断:

openssl s_client -connect google.com:443 2> /dev/null

【讨论】:

  • 谢谢,不过我已经从@that_other_guy 那里得到了答案!
猜你喜欢
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多