Skip to content

Instantly share code, notes, and snippets.

@chasecmiller
Created December 20, 2021 06:41
Show Gist options
  • Save chasecmiller/476327edb00c9f9369b518a115164e7f to your computer and use it in GitHub Desktop.
Save chasecmiller/476327edb00c9f9369b518a115164e7f to your computer and use it in GitHub Desktop.
Check a domain's SSL expiration via a Laravel command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
/**
* In no way is this complete, but should give you a good starting point.
**/
class CheckSsl extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pulse:ssl {url?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import the export file from CVENT';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$url = $this->argument('url');
if (!$url) {
$url = url('/');
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new \Exception('Invalid url.');
}
try {
$orignal_parse = parse_url($url, PHP_URL_HOST);
$get = stream_context_create(array('ssl' => array('capture_peer_cert' => TRUE)));
$read = stream_socket_client('ssl://' . $orignal_parse . ':443', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
//$e = \Carbon\Carbon::parse($certinfo['validTo']);
$valid_from = date(DATE_RFC2822,$certinfo['validFrom_time_t']);
$valid_to = date(DATE_RFC2822,$certinfo['validTo_time_t']);
print_r($valid_from);
print_r($valid_to);
preg_match_all('#DNS:(.*?)[,|$]#', $certinfo['extensions']['subjectAltName'], $domains);
$domains = $domains[1];
} catch (\Throwable $e) {
echo $e->getMessage();
}
// print_r($certinfo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment