TLS / SSL証明書の有効期限を確認するスクリプトはgoogle.comでは機能しますが、https://google.comでは機能しません。

TLS / SSL証明書の有効期限を確認するスクリプトはgoogle.comでは機能しますが、https://google.comでは機能しません。

次のコードを書きましたが、出力は次のようになります。

  • SSL証明書はいつ期限切れになりますか?
  • ブラウザステータスコード

問題は私が挿入したときhttps://www.google.comまたはhttp://www.google.comエラーが発生します。 google.comを使用しようとすると正常に動作します。

どんなアイデアがありますか?


read -p "Please insert the url: " url

certificate_file=$(mktemp)
echo -n | openssl s_client -servername "$url" -connect "$url":443 2>/dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $certificate_file
date=$(openssl x509 -in $certificate_file -enddate -noout | sed "s/.*=\(.*\)/\1/")
date_s=$(date -d "${date}" +%s)
now_s=$(date -d now +%s)
date_diff=$(( (date_s - now_s) / 86400 ))

if [[ "$date_diff" -gt "1" ]]; then

echo "Certificate expires in: $date_diff days"

else
    echo "$url does not use SSL Certificates"

fi

response=$(curl -s -w "%{http_code}" $url)

http_code=$(tail -n1 <<< "$response") 

if [[ "$http_code" == "200" ]]; then

    echo "OK for: $http_code"

elif [[ "http_code" != "200" ]]; then
    echo "HTTP code: $http_code"

elif [[ "http_code" == "408" ]]; then
    echo "Request Timeout"

fi

エラーコードは次のとおりです。

Please insert the url: https:/www.yahoo.com   
unable to load certificate
140381863350720:error:0909006C:PEM routines:get_name:no start line:../crypto/pem/pem_lib.c:745:Expecting: TRUSTED CERTIFICATE
https:/www.yahoo.com does not use SSL Certificates
HTTP code: Regional Redirect302

ベストアンサー1

-connectロゴが使用されますが、openssl s_clientURLhost:portを提供します。したがって、openssl s_client次のエラーが原因でコマンドが失敗します。

s_client: -connect argument or target parameter malformed or ambiguous

ただし、このエラーメッセージを削除すると、2>/dev/null出力には表示されません。コマンドが失敗した場合、openssl s_clientデータはありません$certificate_file。これにより、Expecting: TRUSTED CERTIFICATE出力に表示されるエラーが発生します。

$urlこの問題を解決するには、ホスト名をパラメータとして使用する前にホスト名に変換する必要があります-connect

おすすめ記事