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

纵然是瞬间 提交于 2019-12-05 01:48:09
#! /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("出现错误,请检查域名是否正确或者可达性");

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!