基于python检查SSL证书到期情况代码实例

作者:zack_赵康 时间:2023-06-26 06:23:58 

结合邮件告警和页面展示,再多的域名证书到期情况即可立马知道

代码示例:


# coding: utf-8
# 查询域名证书到期情况

import re
import time
import subprocess
from datetime import datetime
from io import StringIO

def main(domain):
 f = StringIO()
 comm = f"curl -Ivs https://{domain} --connect-timeout 10"

result = subprocess.getstatusoutput(comm)
 f.write(result[1])

m = re.search('start date: (.*?)\n.*?expire date: (.*?)\n.*?common name: (.*?)\n.*?issuer: CN=(.*?)\n', f.getvalue(), re.S)
 start_date = m.group(1)
 expire_date = m.group(2)
 common_name = m.group(3)
 issuer = m.group(4)

# time 字符串转时间数组
 start_date = time.strptime(start_date, "%b %d %H:%M:%S %Y GMT")
 start_date_st = time.strftime("%Y-%m-%d %H:%M:%S", start_date)
 # datetime 字符串转时间数组
 expire_date = datetime.strptime(expire_date, "%b %d %H:%M:%S %Y GMT")
 expire_date_st = datetime.strftime(expire_date,"%Y-%m-%d %H:%M:%S")

# 剩余天数
 remaining = (expire_date-datetime.now()).days

print ('域名:', domain)
 print ('通用名:', common_name)
 print ('开始时间:', start_date_st)
 print ('到期时间:', expire_date_st)
 print (f'剩余时间: {remaining}天')
 print ('颁发机构:', issuer)
 print ('*'*30)

time.sleep(0.5)

if __name__ == "__main__":
 domains = ['www.baidu.com']
 for domain in domains:
   main(domain)

结果示例:

域名: www.baidu.com
通用名: baidu.com
开始时间: 2019-05-09 01:22:02
到期时间: 2020-06-25 05:31:02
剩余时间: 82天
颁发机构: GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
******************************

来源:https://www.cnblogs.com/ZKPython/p/12627100.html

标签:python,SSL,证书
0
投稿

猜你喜欢

  • 关于Python网络爬虫requests库的介绍

    2021-10-20 23:13:52
  • Python编程通过懒属性提升性能

    2022-04-01 09:48:43
  • Python numpy大矩阵运算内存不足如何解决

    2022-08-06 22:38:21
  • matplotlib部件之矩形选区(RectangleSelector)的实现

    2023-02-27 04:07:59
  • Django model 中设置联合约束和联合索引的方法

    2023-09-24 09:14:15
  • python嵌套字典比较值与取值的实现示例

    2023-12-25 01:28:35
  • Python中无限循环需要什么条件

    2023-03-28 09:05:14
  • 提高MySQL查询效率的三个技巧

    2009-02-11 13:19:00
  • Python字符串拼接、截取及替换方法总结分析

    2023-12-03 14:38:57
  • GO语言延迟函数defer用法详解

    2024-02-18 16:56:27
  • 详解go语言单链表及其常用方法的实现

    2024-02-21 03:13:41
  • python连接PostgreSQL数据库的过程详解

    2023-08-24 03:42:31
  • Python3.4学习笔记之常用操作符,条件分支和循环用法示例

    2021-08-24 03:06:43
  • python中get和post有什么区别

    2022-04-17 16:45:15
  • Swift 3.0在集合类数据结构上的一些新变化总结

    2023-10-19 02:35:47
  • .img/.hdr格式转.nii格式的操作

    2023-08-25 04:56:14
  • python自动化测试之破解滑动验证码

    2023-12-01 09:36:25
  • IE9报“DOM Exception: INVALID_CHARACTER_ERR (5)”错误的原因及解决办法

    2011-09-01 19:11:07
  • vue中使用vue-qriously插件生成二维码

    2024-04-27 16:01:31
  • Python中Tkinter组件Menu的具体使用

    2023-09-11 12:46:26
  • asp之家 网络编程 m.aspxhome.com