Skip to content

Instantly share code, notes, and snippets.

@rhenning
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhenning/10495198 to your computer and use it in GitHub Desktop.
Save rhenning/10495198 to your computer and use it in GitHub Desktop.
Script to read information from remote X509 certificates
#!/usr/bin/env ruby
require 'openssl'
require 'socket'
require 'timeout'
DEFAULT_PORT=443
CONNECT_TIMEOUT=5
targets = ARGV.empty? ? ARGF : ARGV
targets.each do |target|
begin
host, port = target.chomp.split(':')
port ||= DEFAULT_PORT
print "#{host}:#{port}\t"
Timeout::timeout(CONNECT_TIMEOUT) do
tcp_sock = TCPSocket.new(host, port)
ssl_sock = OpenSSL::SSL::SSLSocket.new(tcp_sock)
ssl_sock.connect
x509_cert = OpenSSL::X509::Certificate.new(ssl_sock.peer_cert)
puts x509_cert.inspect
end
rescue SystemCallError, SocketError, OpenSSL::SSL::SSLError, Timeout::Error => e
puts "#{e.class} - #{e}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment