Created
January 28, 2019 14:53
-
-
Save mqu/c006d544aef60e7d37bac6824184318e to your computer and use it in GitHub Desktop.
get OpenSSL certificate lifetime in days.
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/ruby | |
# took many ideas from https://spin.atomicobject.com/2016/02/03/ssl-certificate-expiration/ | |
require 'openssl' | |
require 'socket' | |
class OpenSSLCert | |
def initialize hostname | |
@hostname=hostname | |
@cert=self.get_cert hostname | |
end | |
def get_cert hostname | |
tcpsocket = TCPSocket.new(hostname, 443) | |
sslsocket = OpenSSL::SSL::SSLSocket.new(tcpsocket) | |
sslsocket.hostname=hostname | |
sslsocket.connect | |
cert = sslsocket.peer_cert | |
sslsocket.sysclose | |
tcpsocket.close | |
return cert | |
end | |
def cert | |
return @cert | |
end | |
def expire_date | |
@cert.not_after | |
end | |
# how many days before cert expire | |
def lifetime | |
((self.expire_date.to_i - Time.now.to_i) /(3600*24)).to_i | |
end | |
end | |
puts OpenSSLCert.new('gitlab.com').lifetime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment