【问题标题】:How to terminate OpenSSL s_client after connection如何在连接后终止 OpenSSL s_client
【发布时间】:2014-11-03 19:37:27
【问题描述】:

(审阅者:我也知道这是误入超级用户领域,但如果上一个问题偷偷通过...... :))

这与this question 非常相似,但在 Windows (7/8/Server 2008/2012) 环境中:我使用的是 OpenSSL 的 Windows 端口。

我在跑步

openssl s_client -connect 192.168.0.1:443

在命令提示符下,显示证书信息。但是,openssl 之后会等待用户输入;我可以 Ctrl+C 来“中断”输出,或者每次只输入几个字符并按回车键,但我需要自动化这一切——我真的很感兴趣in 是证书信息。

根据上一个问题,我需要一些方法来终止/关闭连接。但是,我尝试在输入文件中使用管道,echoing/typeing 输入混合,似乎没有任何东西可以模拟真实用户。谁能告诉我如何在连接后强制 openssl 退出?

【问题讨论】:

    标签: windows batch-file openssl


    【解决方案1】:

    你可以通过使用管道传入字符“Q”来达到想要的效果。这为脚本提供了一个很棒的单行:

    echo "Q" | openssl s_client -connect host:port
    

    如果您使用的是足够新的 BASH 版本,您还可以使用三重小于重定向而不是管道(有时管道不可用,因为它在 stdin/stdout 上运行):

    openssl s_client -connect host:port <<< "Q"
    

    【讨论】:

      【解决方案2】:

      在空行开头输入字母“Q”将结束活动连接。我已经看到 s_client 进入不执行任何操作的状态,但这是退出会话的记录方法。

      如果您想在批处理模式下执行此操作,只需创建一个带有字母“Q”的文本文件,后跟一个回车符,并将其指向命令的末尾,如下所示:

      openssl s_client -connect host:port < Q.txt
      

      我试过了,效果很好。

      【讨论】:

      • 这看起来很有希望,并且在交互式运行 OpenSSL 时当然可以工作 - 但是有什么方法可以将输入“管道”到命令中,以便我可以强制 OpenSSL 简单地连接、显示信息并退出?
      【解决方案3】:

      我的$profile 中有一个追随者,如果我需要扩展输出,只需使用cert github.com 或cert github.com 15 调用它。工作到现在。

      # $profile
      function test-certificate($domain, $contextLength = 10) {
          
          $domain += ":443"
          echo "q" | openssl s_client -connect $domain | openssl x509 -noout -enddate | sls "notAfter.*"
          echo "q" | openssl s_client -connect $domain | sls "certificate chain" -Context $contextLength
          write-host "~~~" -ForegroundColor darkcyan
          write-host "If needed, pass a desired output length after domainname" -ForegroundColor darkcyan
      }
      Set-Alias cert test-certificate
      

      编辑:要解析unable to get local issuer certificate,请从https://curl.se/docs/caextract.html 下载证书包。我没有以编程方式进行,所以我最终得到了

      function test-certificate($domain, $contextLength = 10) {
          
          $cacertPath = "c:\Users\Admin\tools\cacert.pem" #←EDIT THIS
          $domain += ":443"
          echo "q" | openssl s_client -connect $domain -CAfile $cacertPath  | openssl x509 -noout -enddate | sls "notAfter.*"
          echo "q" | openssl s_client -connect $domain -CAfile $cacertPath  | sls "certificate chain" -Context $contextLength
          Write-Host "~~~" -ForegroundColor darkcyan
          Write-Host "→ If needed, pass a desired output length after domainname" -ForegroundColor darkcyan
          Write-Host "→ To update the list of trusted Certificates, run:" -ForegroundColor darkcyan
          Write-Host "→ Invoke-WebRequest https://curl.se/ca/cacert.pem -OutFile 'c:\Users\Admin\tools\cacert.pem'" -ForegroundColor darkcyan
          Write-Host "~~~" -ForegroundColor darkcyan
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 2015-02-09
        • 2016-06-20
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        • 2011-03-14
        相关资源
        最近更新 更多