Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Mar 4, 2017
0 parents commit de9ddb7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Dan Brown

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# sslcheck

A simple php script to check the expiry of SSL certificates.

### Requirements

* PHP (Including CLI support).
* OpenSSL

This script has been tested only on Ubuntu 16.04+ using PHP7.

### Usage

1. Clone this repo or simply download the `sslcheck` file.
2. Ensure the `sslcheck` file is executable (`chmod a+x sslcheck`).
3. Execute the script `./sslcheck www.example.com`.

If you can't execute the script directly you may need to envoke php (`php sslcheck www.example.com`);

You can check mutiple domains by listing them all out when running the script:

```
sslcheck www.google.com www.example.com www.github.com
```

63 changes: 63 additions & 0 deletions sslcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env php
<?php

function getCert($domain) {
$g = stream_context_create (array("ssl" => array("capture_peer_cert" => true)));
$r = stream_socket_client("ssl://{$domain}:443", $errno, $errstr, 30,
STREAM_CLIENT_CONNECT, $g);
$cont = stream_context_get_params($r);
return openssl_x509_parse($cont["options"]["ssl"]["peer_certificate"]);
}

function getOutputColor($daysLeft) {
if ($daysLeft > 30) return "\e[32m";
if ($daysLeft > 15) return "\e[33m";
return "\e[31m";
}

$domains = array_slice($argv, 1);
$domainCount = count($domains);

if ($domainCount === 0) {
echo "\e[34m\n";
echo "------------------------------------------------------------\n";
echo "Domain SSL Checker \n";
echo "------------------------------------------------------------\n\n";
echo "Usage:\t\t sslcheck [DOMAINS]... \n";
echo "Example:\t sslcheck www.example.com www.google.com \n";
echo "\e[0m\n";
return;
}

$now = new DateTime();
$expiringSoon = [];

echo "\e[34m\n";
echo "------------------------------------------------------------\n";
echo 'Domain SSL Report for ' . $now->format('jS M Y') . "\n";
echo "------------------------------------------------------------\n";
echo "\e[0m\n";


foreach ($domains as $domain) {
$cert = getCert($domain);
$validFrom = new DateTime("@" . $cert['validFrom_time_t']);
$validTo = new DateTime("@" . $cert['validTo_time_t']);
$diff = $now->diff($validTo);
$daysLeft = $diff->invert ? 0 : $diff->days;
if ($daysLeft <= 15) $expiringSoon[] = $domain;
echo getOutputColor($daysLeft);
echo $domain . "\n";
echo "\tValid From:\t " . $validFrom->format('jS M Y') . ' (' . $validFrom->format('Y-m-d H:i:s') . ")\n";
echo "\tValid To:\t " . $validTo->format('jS M Y') . ' (' . $validTo->format('Y-m-d H:i:s') . ")\n";
echo "\tDays Left:\t " . $daysLeft . "\n";
echo "\e[0m\n";
}

$expiringCount = count($expiringSoon);
echo "\e[34m";
echo "------------------------------------------------------------\n";
echo $expiringCount . ' of ' . $domainCount . ' domain' . ($domainCount > 1 ? 's':'') . ' expired or expiring soon' . "\n";
echo "------------------------------------------------------------\n";
echo "\e[0m\n";

0 comments on commit de9ddb7

Please sign in to comment.