检测HTTPS证书到期时间

2021-02-19 22:20

阅读:382

标签:isa   add   xxxxx   port   bin   utc   rap   dom   begin   

#!/usr/bin/env python-devops
# -*- coding: utf-8 -*-
# @Time    : 2019-05-31 16:00
# @Author  : Anthony.long
# @Site    :
# @File    : check_https_dated.py
# @Software: PyCharm


import ssl, socket
import requests
from dateutil import parser
import pytz


def get_domain_content(domain):
    requests.packages.urllib3.disable_warnings()
    url = ‘https://‘ + domain
    response = requests.get(url, verify=False).headers
    print(response)


def get_my_domain(mydomain):
    try:
        my_addr = socket.getaddrinfo(mydomain, None)
        c = ssl.create_default_context()
        s = c.wrap_socket(socket.socket(), server_hostname=mydomain)
        s.connect((mydomain, 443))
        my_cert = s.getpeercert()
        get_my_cert_dated(mydomain, my_cert, my_addr)
    except ssl.CertificateError and socket.gaierror as e:
        pass


def get_my_cert_dated(domain, certs, my_addr):
    cert_beginning_time = parser.parse(certs[‘notBefore‘]).astimezone(pytz.utc)
    cert_end_time = parser.parse(certs[‘notAfter‘]).astimezone(pytz.utc)

    print(‘域名:(%s)        证书创建时间:%s       证书失效时间: %s      IP地址: %s ‘ % (
        domain, cert_beginning_time, cert_end_time, my_addr[0][4][0]))


def read_domain_files():
    with open(‘/xxxxxxxxx/domain.txt‘, ‘r‘,
              encoding="utf-8") as file:
        for domain in file:
            get_my_domain(domain.strip())


if __name__ == "__main__":
    read_domain_files()

检测HTTPS证书到期时间

标签:isa   add   xxxxx   port   bin   utc   rap   dom   begin   

原文地址:https://www.cnblogs.com/ipyanthony/p/12925140.html


评论


亲,登录后才可以留言!