Skip to content

Instantly share code, notes, and snippets.

@mqu
Created January 28, 2019 14:53
Show Gist options
  • Save mqu/c006d544aef60e7d37bac6824184318e to your computer and use it in GitHub Desktop.
Save mqu/c006d544aef60e7d37bac6824184318e to your computer and use it in GitHub Desktop.
get OpenSSL certificate lifetime in days.
#!/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