-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssl-check
executable file
·83 lines (69 loc) · 1.72 KB
/
ssl-check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env ruby
#
# ssl-check
#
# Checks for expiring SSL certificates, specified on the command line.
#
# Usage:
#
# $ ssl-check hosts.txt
#
# …where hosts.txt contains e.g.:
#
# example.com
#
# Sample output:
#
# $ ssl-check
# SSL certificate for example.com is expiring within 21 days, on 2017-03-24
#
# If any of the certificates are expired/expiring, the script's exit
# status will be 1; if all certs are valid, the exit status will be 0.
require "net/http"
require "openssl"
require "date"
# How many days before expiry should we begin to warn?
CUTOFF = 14
# If true, information about all certificates will be output, not just
# the ones that are close to expiring
VERBOSE = false
unless ARGV[0]
abort "Usage: ssl-check HOSTS_FILE"
end
domains = File.read(ARGV[0]).lines.map { |h| h.strip.downcase }
unless domains.length > 0
abort "No domains specified in hosts file"
end
problems = false
domains.each do |domain|
uri = URI.parse("https://" + domain)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
cert = nil
http.start do |h|
cert = h.peer_cert
end
unless cert
problems = true
puts "#{domain}: couldn't find SSL certificate"
next
end
expiry = cert.not_after
expiry_in_days = expiry.to_date - Date.today
if expiry_in_days < CUTOFF
problems = true
puts "#{domain}: SSL certificate is expiring in #{expiry_in_days.to_i} days, on #{expiry.strftime("%Y-%m-%d")}"
next
end
# At this point, we must have a valid certificate
if VERBOSE
puts "#{domain}: SSL certificate is valid and doesn't expire until #{expiry.strftime("%Y-%m-%d")}"
end
end
if problems
exit 1
else
puts "All's well."
exit 0
end