美文网首页
通过python获取SSL证书到期时间

通过python获取SSL证书到期时间

作者: 乐维_lwops | 来源:发表于2023-04-19 14:41 被阅读0次

    在前面的文章中曾介绍过如何通过openssl命令获取SSL证书的到期时间:通过zabbix监控ssl证书到期时间

    有人反馈实践中这种方式存在缺陷,可能会出现部分域名证书无法获取的情况,报错如下:

    140323981043600:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:707:Expecting: TRUSTED CERTIFICATE0

    接下来介绍另一种解决方式——通过python获取SSL证书到期时间,代码如下:

    #!/bin/env python3import sslimport socketimport datetimeimport syshostname = 'baidu.com'port = 443context = ssl.create_default_context()with socket.create_connection((hostname, port)) as sock:    with context.wrap_socket(sock, server_hostname=hostname) as sslsock:        cert = sslsock.getpeercert()expiry_date_str = cert['notAfter']expiry_date_obj = datetime.datetime.strptime(expiry_date_str, '%b %d %H:%M:%S %Y %Z')days_left = (expiry_date_obj - datetime.datetime.now()).daysprint(days_left)

    模板制作方式在此不做描述,可参考之前的文章,通过zabbix监控ssl证书到期时间

    以上就是这一期的分享内容。大家好,我是乐乐,专注运维技术研究与分享,关注我,了解更多运维小知识。如有问题,还可以到乐维社区进行留言提问,与广大运维技术爱好者共同探讨。

    相关文章

      网友评论

          本文标题:通过python获取SSL证书到期时间

          本文链接:https://www.haomeiwen.com/subject/gxrljdtx.html