Created
January 29, 2020 14:50
-
-
Save kwando/28b62d51008cf178da0973852c1b3323 to your computer and use it in GitHub Desktop.
Check certificate expiration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'date' | |
require 'time' | |
domain = ARGV[0] | |
if !domain | |
STDERR.puts "no domain given" | |
exit(-1) | |
end | |
dates = `openssl s_client -connect #{domain}:443 2>/dev/null </dev/null | openssl x509 -noout -dates` | |
dates = dates.split("\n").map(&:strip).map{|date| date.split("=")}.map{|key, date| [key, Time.parse(date)]}.to_h | |
def date_diff(d1, d2) | |
(d1.to_date - d2.to_date).to_i | |
end | |
puts '=== SSL Cert Check ===' | |
puts "Domain: #{domain}" | |
days_used = date_diff(Date.today, dates['notBefore']) | |
days_left = date_diff(dates['notAfter'], Date.today) | |
duration = date_diff(dates['notAfter'], dates['notBefore']) | |
printf "#{days_used} of #{duration} days used (%.0f%%)\n", days_used.to_f/duration*100 | |
puts "#{days_left} days left" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment