python脚本检查ssl证书过期时间

#! /usr/bin/python
# -*- coding:utf-8 -*-
# Author: panb

##此脚本用来获取https证书过期时间,需要先执行pip3 install pyopenssl
import argparse;
from urllib3.contrib import pyopenssl as reqs;
from datetime import datetime;

#命令行参数
parser = argparse.ArgumentParser(description='本脚本获取https证书到期时间');
parser.add_argument('-w', '-www', metavar='https网站,如www.jcici.com',required=True, dest='sites', nargs='+', help='输入监控的https网站')
args = parser.parse_args()

#公网验证
def get_notafter(www):
    cert = reqs.OpenSSL.crypto.load_certificate(reqs.OpenSSL.crypto.FILETYPE_PEM, reqs.ssl.get_server_certificate((www, 443)));

    notafter = datetime.strptime(cert.get_notAfter().decode()[0:-1], '%Y%m%d%H%M%S');
    remain_days = notafter - datetime.now();
    print(www, '证书到期天数是:', remain_days.days);

#输出结果
try:
    for site in args.sites:
        get_notafter(site);
except Exception as e:
    print("出现错误,请检查域名是否正确或者可达性");

猜你喜欢

转载自www.cnblogs.com/jcici/p/11895592.html