<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>barnoid.org.uk - The website of Barney Livingston : Tag openssl, everything about openssl</title>
    <link>http://barnoid.org.uk</link>
    <atom:link type="application/rss+xml" rel="self" href="http://barnoid.org.uk/tag/openssl.rss"/>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Certificate Expiry Checking Script</title>
      <description>&lt;p&gt;Here's a script I wrote a while ago for checking a bunch of domains to see if their certificates have expired using openssl. Just add your domains to the &lt;span class="commandline"&gt;@domains&lt;/span&gt; list. At work we have a cronjob that does this every day:&lt;/p&gt;

&lt;p&gt;&lt;span class="commandline"&gt;checkcerts -d 31 | mail -e -s"SSL Certificates Expiring Within a Month" systems@...&lt;/span&gt;&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;#!/usr/bin/perl -w

# checkcerts
# Barney Livingston 2008-11-18

use strict;
use Date::Parse;
use Date::Format;
use POSIX qw(floor);
use Getopt::Long;

my $days = 9999999999; #about 27 million years should be enough
my $help;

GetOptions( &amp;quot;days=i&amp;quot; =&amp;gt; \$days,
            &amp;quot;help&amp;quot; =&amp;gt; \$help );

if ($help) {
  print &amp;lt;&amp;lt;&amp;quot;END&amp;quot;;
Usage: checkcerts [options]
  --days -d &amp;lt;days&amp;gt;   Only show certificates due to expire within &amp;lt;days&amp;gt; days.
END
  exit 0;
}

my @domains = ( &amp;quot;example.com&amp;quot; ); # Your list of domains goes here.

my $time = time;

foreach my $domain (@domains) {

  my $tries = 3;
  my $date = &amp;quot;&amp;quot;;
  my $cn = &amp;quot;&amp;quot;;
  my $expdays = 0;
  my $x509 = &amp;quot;&amp;quot;;

  while ($tries &amp;gt; 0) {
    $x509 = `echo Q | openssl s_client -connect $domain:443 2&amp;gt;&amp;amp;1 | openssl x509 -noout -text`;

    $date = &amp;quot;ERROR&amp;quot;;
    if ($x509 =~ /Not After : (... .. ..:..:.. .... ...)\n/) {
      $date = str2time($1);
    }
    $x509=~ /Subject:.+CN=(.*?)[\/\n]/;
    $cn = $1;

    if ($date eq &amp;quot;ERROR&amp;quot;) {
       $tries--;
    } else {
       $tries = 0;
    }
  }


  if ($date eq &amp;quot;ERROR&amp;quot;) {
    print &amp;quot;Failed to get a useful response from $domain\n&amp;quot;;
  } else {
    $expdays = floor(($date - $time) / (3600 * 24));
    my $expired = &amp;quot;&amp;quot;;
    $expired = &amp;quot; EXPIRED&amp;quot; if ($expdays &amp;lt; 0);
    if ($expdays &amp;lt; $days) {
      print time2str(&amp;quot;%Y-%m-%d %T&amp;quot;, $date) . &amp;quot; -$expired $expdays days - $cn\n&amp;quot;;
    }
  }

}&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
      <pubDate>Sun, 20 Jun 2010 14:01:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:e2dc6430-6a8d-4fb9-b321-18c3920db860</guid>
      <comments>http://barnoid.org.uk/certificate-expiry-checking-script#comments</comments>
      <category>Articles</category>
      <category>openssl</category>
      <category>script</category>
      <category>perl</category>
      <category>certificate</category>
      <category>ssl</category>
      <category>sysadmin</category>
      <link>http://barnoid.org.uk/certificate-expiry-checking-script</link>
    </item>
  </channel>
</rss>

