Skip to content

Instantly share code, notes, and snippets.

@facelordgists
Created February 1, 2019 23:09
Show Gist options
  • Save facelordgists/4e7444e3fa766cc6dba9872a5ff33441 to your computer and use it in GitHub Desktop.
Save facelordgists/4e7444e3fa766cc6dba9872a5ff33441 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
function get_ssl_domain_expiration($ssl_domain){
$i = 0;
$ssl_domain_data = array();
$domain = parse_url($ssl_domain, PHP_URL_HOST);
$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);
$cert = stream_context_get_params($r);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('America/Los_Angeles'));
$expiration_date = new DateTime(date('Y-m-d', $certinfo['validTo_time_t']), new DateTimeZone('America/Los_Angeles'));
$interval = $current_date->diff($expiration_date);
$days_left = (int)$interval->format('%a');
$ssl_domain_data = array(
'domain' => $domain,
'expiration_date' => $expiration_date->format('m/d/Y'),
'days_left' => $days_left,
);
return $ssl_domain_data;
}
if( $argv[1] ){
$normalized_domain = parse_url('http://' . str_replace(array('https://', 'http://'), '', $argv[1]), PHP_URL_HOST);
// echo "normalized_domain " . $normalized_domain . "\n";
if(filter_var('http://'.$normalized_domain, FILTER_VALIDATE_URL)){
$data = get_ssl_domain_expiration('https://'.$normalized_domain);
echo $data['expiration_date']." - ";
echo $data['domain']." has ";
echo $data['days_left']." days left";
echo "\n";
} else {
echo "\"" . $argv[1] . "\" is not a valid domain name.\n";
}
} else {
echo "You must supply a valid domain name.\n";
echo "Example:\n";
echo "php check-domain-expiration.php www.example.com\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment