获取证书过期时间

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
# 使用前安装  pip install alibabacloud_alidns20150109==2.0.2   pyOpenSSL
import sys
import json
import ssl
import socket
import datetime
from typing import List
from OpenSSL import crypto
from alibabacloud_alidns20150109.client import Client as Alidns20150109Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_alidns20150109 import models as alidns_20150109_models
from alibabacloud_tea_util import models as util_models



class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: "xxxxxxxxxxxxxxxx",
        access_key_secret: "xxxxxxxxxxxxxxxxx",
    ) -> Alidns20150109Client:
        """
        使用AK&SK初始化账号Client
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )
        # 访问的域名
        config.endpoint = f'alidns.cn-beijing.aliyuncs.com'
        return Alidns20150109Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        data_list = []
        for number in range(1, 10):
            client = Sample.create_client('xxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxx')
            describe_domain_records_request = alidns_20150109_models.DescribeDomainRecordsRequest(
                domain_name='test.com',
                page_number=number,
                page_size=200
            )
            runtime = util_models.RuntimeOptions()
            resp = client.describe_domain_records_with_options(describe_domain_records_request, runtime)
            domain_num = resp.body.total_count
            # domain_rr = for i in resp.body.domain_records.record
            dta = resp.body.domain_records.record
            for i in dta:
                if "@" in i.rr:
                    continue
                data_list.append(i.rr+"."+i.domain_name)

        for data_str in data_list:
            try:
                context = ssl.SSLContext(ssl.PROTOCOL_TLS)
                sock = socket.socket(socket.AF_INET)
                sock.settimeout(5)
                wrappedSocket = context.wrap_socket(socket.socket(socket.AF_INET), server_hostname=data_str)
                wrappedSocket.connect((data_str, 443))
                pem_cert = ssl.DER_cert_to_PEM_cert(wrappedSocket.getpeercert(True))
                wrappedSocket.close()
                io_cert = crypto.load_certificate(crypto.FILETYPE_PEM, pem_cert)
                notAfter = io_cert.get_notAfter().decode()[:-1]
                ssltime = datetime.datetime.strptime(notAfter, '%Y%m%d%H%M%S')
                nowtime = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),'%Y-%m-%d %H:%M:%S')
                usedtime = (ssltime - nowtime).days
                if usedtime < 1000:
                    with open('info.txt', 'a', encoding='utf-8') as f:
                        f.write(json.dumps({"域名(443)": data_str, "距离证书过期的天数": usedtime, "time": datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')}, ensure_ascii=False)+'\n')
            except Exception as e:
                with open('error.txt', 'a', encoding='utf-8') as f:
                    f.write("域名"+data_str+"获取失败"+"\n")
            print("runing")
        print("结束")







if __name__ == '__main__':
    Sample.main(sys.argv[1:])

 

posted @ 2022-07-21 18:45  李瑞鑫  阅读(118)  评论(0编辑  收藏  举报