All pastes #605242 Raw Edit

Something

public text v1 · immutable
·published 2007-07-06 01:28 UTC
rendered paste body
import os, sys, time, smtplib, getopt

email_from = "noreply@qqwith.us"
email_to = "nick@qqwith.us"
email_subject = "Report for expired certs"
smtp_server = "smtp.qqwith.us"
default_dir = "/nickspants"
send_email = 0
verbose = 0

def usage():
  print "Usage:"
  print " -v             - verbose"
  print " -E <email> - Email address to send report"
  print " -e             - Send email report"
  print " -d <dir>   - Directory to scan"


try:
  opts, args = getopt.getopt(sys.argv[1:], "vd:eE:h", [])
except getopt.GetoptError:
  usage()
  sys.exit(2)

for opt, arg in opts:
  if opt == "-v":
    verbose = 1
  if opt == "-h":
    usage()
    sys.exit(0)
  if opt == "-E":
    email_to = arg
    send_email = 1
  if opt == "-e":
    send_email = 1
  if opt == "-d":
    default_dir = arg

data = ""
for file in os.listdir(default_dir):
  if file[-3:] != "crt":
    continue
  crt = open(os.path.join(default_dir, file), "r")
  for line in crt.readlines():
    if line.find("Not After") != -1:
      line = line.strip()
      tokens = line.split(": ")
      expire = time.mktime(time.strptime(tokens[1], "%b %d %H:%M:%S %Y %Z"))
      now = time.time()
      nextweek = now + 7*24*60*60
      if (expire <= now):
        info = "%s has expired" % file
        data.append(info)
        if verbose:
          print info
      elif (expire <= nextweek):
        info = "%s is going to expire in %d days" % (file, int((expire-now)/(24*60*60)))
        data.append(info)
        if verbose:
          print info


if data:
  email_body = """From: %s
To: %s
Subject: %s

%s
""" % (email_from, email_to, email_subject, "\n".join(data))

  smtp = smtplib.SMTP(smtp_server)
  smtp.sendmail(email_from, email_to, email_body)