Project to scan hosts and check the validity of SSL certificates

From MyWiki
Jump to: navigation, search

Reference:- https://www.w3schools.com/jsref/met_table_insertrow.asp the sample code from this link
Code at 04 April 2019

#!/usr/bin/env php
<?php
 
error_reporting(E_ERROR | E_PARSE);
 
function getCert($domain) {
    $g = stream_context_create (array("ssl" => array("capture_peer_cert" => true, "verify_peer" => false)));
    $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);
 
 
$now = new DateTime();
$expiringSoon = [];
$errors = [];
 
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) {
function run ($domain) {
    $cert = getCert($domain);
echo "Debug follows the domain\n";
echo $domain;
echo "\nEnd of domain\n";
   $cert = getCert($domain);
    if (!$cert) {
        $errors[] =  $domain . " :: FAILED TO GET CERTIFICATE INFORMATION\n";
        return 1 ;
    }
    $validFrom = new DateTime("@" . $cert['validFrom_time_t']);
    $validTo = new DateTime("@" . $cert['validTo_time_t']);
 
$now = new DateTime();
 
$FROM = $validFrom->format('jS M Y') ;
$TO = $validTo->format('jS M Y');
$diff = $now->diff($validTo);
$daysLeft = $diff->invert ? 0 : $diff->days;
 
$stack = array($FROM, $TO,$daysLeft);
//print_r($stack);
 
 
//   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";
// Create an array to return the data
return $stack;
}
 
 
$reto = run("bbc.co.uk");
//print_r($reto);
$days = array_pop($reto);
echo "the days are : " . $days . "\n";
$expiry = array_pop($reto);
echo "the expiry is : " . $expiry . "\n";
$start = array_pop($reto);
echo "the start is : " . $start . "\n";