Script to get the expiration date of the SSL certificate

I referred to here. http://qiita.com/uemura/items/a3a0937f77494e62213c

Assuming to import and use

When used from a command

$ ./cert_expires.py example.com google.com
example.com: 1093 days
google.com: 76 days

sert_expires.py


#!/usr/bin/env python


import sys
import re
import subprocess
import datetime


def parse_result_date_string(date_string):
    """
    convert date string to datetime
    >>> date_string = "Feb  2 05:47:50 2015 GMT"
    >>> d = parse_result_date_string(date_string)
    >>> d
    datetime.datetime(2015, 2, 2, 5, 47, 50)
    """
    d = datetime.datetime.strptime(date_string, '%b %d %H:%M:%S %Y %Z')
    return d


re_start = re.compile(r'Not Before: (.+)')
re_end = re.compile(r'Not After : (.+)')


def get_date_strings_from_result(result):
    """
    :return: cert start date, cert expire date
    >>> text = "Not Before: Feb  2 05:47:47 2015 GMT\\n" \\
    ...        "Not After : Feb  4 16:03:29 2016 GMT"
    >>> get_date_strings_from_result(text)
    ('Feb  2 05:47:47 2015 GMT', 'Feb  4 16:03:29 2016 GMT')
    """
    rr_start = re_start.search(result)
    rr_end = re_end.search(result)
    return rr_start.group(1) if rr_start else None, \
           rr_end.group(1) if rr_end else None


def get_cert_start_expire_date(domain):
    """
    :return: cert start date, cert expire date
    """
    command = "openssl s_client -connect {}:443 < /dev/null " \
              "2> /dev/null | openssl x509 -text | grep Not"

    command = command.format(domain)
    out = subprocess.check_output(command, shell=True)
    ss, es = get_date_strings_from_result(out)
    return parse_result_date_string(ss), parse_result_date_string(es)


def get_cert_expire_delta(domain):
    """
    time delta cert expire date and now
    """
    start_date, expire_date = get_cert_start_expire_date(domain)
    return expire_date - datetime.datetime.now()


def main():
    for domain in sys.argv[1:]:
        delta = get_cert_expire_delta(domain)
        print('{}: {} days'.format(domain, delta.days))


if __name__ == '__main__':
    main()

Recommended Posts

Script to get the expiration date of the SSL certificate
Obtaining SSL certificate expiration date
Script to change the description of fasta
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
To get the path of the currently running python.exe
Get the update date of the Python memo file.
Python script to get a list of input examples for the AtCoder contest
Template of python script to read the contents of the file
Get the absolute path of the script you are running
Get the return code of the Python script from bat
Get to know the feelings of gradient boosting trees
I created a Slack bot that confirms and notifies AWS Lambda of the expiration date of an SSL certificate
The story of switching from WoSign to Let's Encrypt for a free SSL certificate
Get the number of digits
Try to get the function list of Python> os package
I tried to get the location information of Odakyu Bus
I want to get the operation information of yahoo route
Keras I want to get the output of any layer !!
To get the name of the primitive etc. generated immediately before
Get the source of the page to load infinitely with python.
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
I tried to create a Python script to get the value of a cell in Microsoft Excel
I want to get the name of the function / method being executed
Set the last modified date of the child file to the modified date of the parent directory
Get the number of views of Qiita
Shell script (Linux, macOS) that outputs the date of the last week
I tried to get the index of the list using the enumerate function
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
Conditional branch due to the existence of a shell script file
Get the site update date seriously
[Python] How to get the first and last days of the month
Get the script path in Python
How to get the Python version
Get the attributes of an object
[Python] Get the update date of a news article from HTML
How to get the vertex coordinates of a feature in ArcPy
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
Create a function to get the contents of the database in Go
Supplement to the explanation of vscode
Get the title and delivery date of Yahoo! News in Python
PhytoMine-I tried to get the genetic information of plants with Python
I tried to get the batting results of Hachinai using image processing
How to get the date and time difference in seconds with python
Click the Selenium links in order to get the elements of individual pages
[Linux] Command to get a list of commands executed in the past
I tried to get the authentication code of Qiita API with Python.
Try to get the road surface condition using big data of road surface management
I tried to get the RSS of the top song of the iTunes store automatically
Get the song name from the title of the video you tried to sing
I tried to get the movie information of TMDb API with Python
Get the return value of an external shell script (ls) with python3
The story of trying to reconnect the client
10 methods to improve the accuracy of BERT
How to check the version of Django
The story of adding MeCab to ubuntu 16.04
Get the column list & data list of CASTable
Get the minutes of the Diet via API
Get the path to the systemd unit file
How to get colored output to the console
Get the value of the middle layer of NN
Get the last day of the specified month