https证书检查python脚本

#!/usr/bin/env python3  
  
import socket
import ssl

import pytz
import requests
from dateutil import parser

requests.packages.urllib3.disable_warnings()  
  
try:  
    _create_unverified_https_context = ssl._create_unverified_context  
except AttributeError:  
    # Legacy Python that doesn't verify HTTPS certificates by default  
    pass  
else:  
    # Handle target environment that doesn't support HTTPS verification  
    ssl._create_default_https_context = _create_unverified_https_context  
  
  
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:  
        socket.setdefaulttimeout(5)  
        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' % (domain,  cert_end_time))  
  
  
def read_domain_files():  
    with open('./url.txt', 'r',  
              encoding="utf-8") as file:  
        for domain in file:  
            try:  
                get_my_domain(domain.strip())  
            except Exception as e:  
                print('域名: (%s) 检查异常,返回结果: %s' %(domain.strip(), e))
  
  
if __name__ == "__main__":  
    read_domain_files()

在当前目录下创建一个url.txt文件,将需要检测域名写入文件内

[root@test src]# cat url.txt 
www.baidu.com
www.360.com
editor.csdn.net

之后执行脚本输出结果

[root@test src]# python3 ssl_check.py 
域名:(www.baidu.com)  证书失效时间: 2023-08-06 05:16:01+00:00
域名:(www.360.com)  证书失效时间: 2023-04-29 23:59:59+00:00
域名:(editor.csdn.net)  证书失效时间: 2023-12-01 23:59:59+00:00
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值