检查阿里云ssl证书到期情况

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # @Time    : 2019-06-10 16:00
 4 # @Author  : Anthony.long
 5 # @Site    :
 6 # @File    : check_ssl_dated.py
 7 # @Software: PyCharm
 8 
 9 
10 # 查询域名证书到期情况
11 
12 import re
13 import time
14 import subprocess
15 from datetime import datetime
16 from io import StringIO
17 
18 
19 def main(domain):
20     f = StringIO()
21     start_time = time.strftime("%Y-%m-%d %X", time.localtime())
22 
23     comm = f"curl -Ivs https://{domain} --connect-timeout 5"
24     result = subprocess.getstatusoutput(comm)
25     f.write(result[1])
26 
27     end_time = time.strftime("%Y-%m-%d %X", time.localtime())
28 
29     start_date = re.search('(start date:.*?
)', f.getvalue(), re.S).group().strip().split(': ')[1]
30     expire_date = re.search('(expire date:.*?
)', f.getvalue(), re.S).group().strip().split(': ')[1]
31     subjectAltName_name = re.search('(subjectAltName:.*?
)', f.getvalue(), re.S).group().strip().split(': ')[1]
32     issuer = re.search('(issuer:.*?
)', f.getvalue(), re.S).group().strip().split(': ')[1]
33 
34     # # time 字符串转时间数组
35     start_date = time.strptime(start_date, "%b %d %H:%M:%S %Y GMT")
36     start_date_st = time.strftime("%Y-%m-%d %H:%M:%S", start_date)
37     # # datetime 字符串转时间数组
38     expire_date = datetime.strptime(expire_date, "%b %d %H:%M:%S %Y GMT")
39     expire_date_st = datetime.strftime(expire_date, "%Y-%m-%d %H:%M:%S")
40 
41     # # 剩余天数
42     remaining = (expire_date - datetime.now()).days
43 
44     print('域名:', domain)
45     print('通用名:', subjectAltName_name)
46     print('证书开始使用时间:', start_date_st)
47     print('证书到期时间:', expire_date_st)
48     print(f'证书剩余可用时间: {remaining}天')
49     print('颁发机构:', issuer)
50     print('*' * 30)
51 
52     time.sleep(0.5)
53 
54 
55 if __name__ == "__main__":
56     with open('domains.txt','r',encoding="utf-8") as file:
57     # with open('onlineDomains.txt', 'r', encoding="utf-8") as file:
58         for domain in file:
59             main(domain.strip())
原文地址:https://www.cnblogs.com/ipyanthony/p/10999821.html