Python 版本的 SSL 证书过期巡检脚本

2023-08-0411:30:39域名主机证书Comments1,145 views字数 1854阅读模式

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

完整代码在文末文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

思路

导入相关模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

首先我们创建一个 domain.txt 用来存放要检查的域名和对应的 IP 地址文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

我们读取该文件,把里面的域名和对应的每个 ip 取出来,并存放到字典 domains 里面文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

取出来之后我们循环遍历字典,去获取每个域名对应的证书信息(ssl_connect 函数)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

ssl_connect 函数返回一个字典 skt_info,包含当前连接的域名、ip 地址和证书过期时间文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

然后我们调用 check_cert_time 函数进行证书有效期检查和提示文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

check_cert_time 函数内容如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

最后我们执行一下代码,看看结果如何文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

Python 版本的 SSL 证书过期巡检脚本文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html

完整代码
import sslimport socketimport timefrom datetime import datetime

def ssl_connect(domain, ip):    # 设置socket的超时时间为5秒    socket.setdefaulttimeout(5)    # 创建默认的SSL上下文    context = ssl.create_default_context()    # 创建一个SSL套接字    skt = context.wrap_socket(socket.socket(), server_hostname=domain)    try:        # 建立SSL连接        skt.connect((ip, 443))
        # 获取证书过期时间        end_date = skt.getpeercert()['notAfter'].strip(' GMT')
        # 创建一个字典,存储本次连接中的域名、IP 地址和证书过期时间信息        skt_info = {'domain': domain, 'ip': ip, 'end_date': end_date}    except ssl.CertificateError as e:        cert = e    except socket.timeout:        cert = 'Connect refused'    except ConnectionResetError as e:        cert = 'Connect reset' + str(e)    except socket.gaierror as e:        cert = 'Connnect gaierror'    finally:        # 关闭SSL套接字        skt.close()    return skt_info

def check_cert_time(info):    # 获取当前时间戳    current_timestamp = int(time.time())
    # 将证书过期时间转换成时间戳    date_object = datetime.strptime(info['end_date'], "%b %d %H:%M:%S %Y")    end_timestamp = int(date_object.timestamp())
    # 计算剩余天数    remain_day = (end_timestamp - current_timestamp) / 86400
    # 打印域名、IP 地址和证书过期时间信息    print(f"域名:{info['domain']},ip 地址:{info['ip']},证书过期时间:{info['end_date']}")
    # 根据剩余天数进行不同的提示    # 如果证书过期时间减去当前时间的天数小于七天的话,则提示需要准备更换证书了    if 0 < remain_day < 7:        print('剩余时间小于七天!请及时更换证书!')    elif remain_day < 0:        print('证书已过期!请及时更换证书!')    else:        print(f"剩余天数为:{remain_day:.2f}天\n")


if __name__ == "__main__":    domains = {}
    with open('domain', 'r', encoding='utf-8') as file:        for line in file:            domain, ip_pool = line.strip().split(':')            domains[domain] = ip_pool.split(',')
    info = [ssl_connect(domain, ip) for domain, ip_pool in domains.items() for ip in ip_pool]
    [check_cert_time(i) for i in info]
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymzj/52579.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymzj/52579.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定