{"id":740,"date":"2022-04-13T01:17:58","date_gmt":"2022-04-13T00:17:58","guid":{"rendered":"http:\/\/thejoyofstick.com\/blog\/?p=740"},"modified":"2022-04-13T01:17:58","modified_gmt":"2022-04-13T00:17:58","slug":"how-to-check-an-ssl-certificate-expiration-date-from-the-command-line","status":"publish","type":"post","link":"https:\/\/thejoyofstick.com\/blog\/2022\/04\/13\/how-to-check-an-ssl-certificate-expiration-date-from-the-command-line\/","title":{"rendered":"How to check an SSL certificate expiration date from the command line"},"content":{"rendered":"<p>Some days ago one of my sites certificate expired. I didn&#8217;t pay attention to the <a href=\"https:\/\/letsencrypt.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Let&#8217;s Encrypt<\/a> periodic warning emails &#8211; I&#8217;m using the <a href=\"https:\/\/letsencrypt.org\/docs\/challenge-types\/#dns-01-challenge\" target=\"_blank\" rel=\"noreferrer noopener\">DNS-01 challenge<\/a>.<\/p>\n<p>I must create a simple script to check how many days are left and install it as a cron job.<\/p>\n<p>After some search I found that <code>curl <\/code>and <code>date<\/code> could do it.<\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"brush: bash; gutter: false; title: ; notranslate\" title=\"\">\n$ curl https:\/\/thejoyofstick.com -vI --stderr - | grep &quot;expire date:&quot; | cut -d: -f 2-\nJun 25 17:26:52 2022 GMT\n\n$ date --date='Jun 25 17:26:52 2022 GMT' '+%s'\n1656178012\n\n$ date '+%s'\n1649805603\n\n$ echo '(1656178012-1649805603)\/86400' | bc\n73\n<\/pre>\n<\/div>\n<p>We now have a way to calculate how many days are left. <br \/>You can build a bash script with these and, after checking for a countdown days threshold, send  yourself a proper alert email.<\/p>\n<p>For myself I&#8217;m using a <a href=\"https:\/\/www.perl.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">perl<\/a> script based on this one<\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/env perl\n\nuse strictures 2;\nuse IO::Socket::SSL;\nuse Net::SSLeay;\nuse Date::Simple ( 'date', 'today' );\n\nmy $site = shift || 'example.com';\n\nmy $client = IO::Socket::SSL-&gt;new(&quot;$site:443&quot;)\n  or die &quot;error=$!, ssl_error=$SSL_ERROR&quot;;\nmy $cert = $client-&gt;peer_certificate();\nmy $time = Net::SSLeay::X509_get_notAfter($cert);\nmy $asn_t = Net::SSLeay::P_ASN1_TIME_get_isotime($time);\nmy ($date) = split \/T\/, $asn_t;\nmy $diff = date($date) - today;\nmy $warning_time = 15;\n\nprint &quot;Expire date: $date\\n&quot;;\nprint &quot;How many days left: $diff\\n&quot;;\nprint &quot;Warning before $warning_time days\\n&quot;;\n\n<\/pre>\n<\/div>\n<p>Added a personal email module to send a message if <code>$diff<\/code> is less than <code>$warning_time<\/code> and then <em>cron<\/em> with it.<\/p>\n<p>Enjoy!<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some days ago one of my sites certificate expired. I didn&#8217;t pay attention to the Let&#8217;s Encrypt periodic warning emails &#8211; I&#8217;m using the DNS-01 challenge. I must create a simple script to check how many days are left and&hellip; <a href=\"https:\/\/thejoyofstick.com\/blog\/2022\/04\/13\/how-to-check-an-ssl-certificate-expiration-date-from-the-command-line\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[90,88,89,20],"class_list":["post-740","post","type-post","status-publish","format-standard","hentry","tag-certificate","tag-openssl","tag-ssl","tag-ubuntu-2"],"jetpack_shortlink":"https:\/\/wp.me\/pcmwlL-bW","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/posts\/740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/comments?post=740"}],"version-history":[{"count":0,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/posts\/740\/revisions"}],"wp:attachment":[{"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/media?parent=740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/categories?post=740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/tags?post=740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}