背景
有的公司的网站的证明书每年或者几个月需要更新一下,更新的时候总是忘记哪些需要更新,哪些不需要更新。基于这种场景下,用python做了一个通过网站的URL取得此网站的证明书的有效情报
工具
python
具体实行
需要安装以下包文件
#需要安装以下包文件 pip install pyopenssl pip install datetime pip install xlrd pip install xlwt pip install xlutils pip install requests pip install json pip install os
python代码
#定期查看网页的证明书的有效时间 from urllib3.contrib import pyopenssl import datetime import xlrd import xlwt from xlutils.copy import copy from xlwt import Style import requests import json import os def get_expire(https_url): try: conn = pyopenssl.ssl.create_connection((https_url, 443)) sock = pyopenssl.ssl.SSLContext(pyopenssl.ssl.PROTOCOL_SSLv23).wrap_socket(conn, server_hostname=https_url) cert = pyopenssl.ssl.DER_cert_to_PEM_cert(sock.getpeercert(True)) data = pyopenssl.OpenSSL.crypto.load_certificate(pyopenssl.OpenSSL.crypto.FILETYPE_PEM, cert) print(data.get_notAfter().decode()[0:-1]) expire_time = datetime.datetime.strptime(data.get_notAfter().decode()[0:-1], '%Y%m%d%H%M%S') expire_days = (expire_time - datetime.datetime.now()).days return True, str(expire_time),expire_days except Exception as e: print(e) return False, "",0 ''' Slack 通知 ''' def sendSlackMessage(expire_time,url): # URL tokenUrl = "https://slack.com/api/chat.postMessage" headers = {'Content-Type': 'application/json; charset=UTF-8','Authorization': 'Bearer xoxb-1314773616387-1299839993671-WgxnrfpqsPZJbuxH3CGgVELJ'} # パラメータ設定 item_data ={ "channel":"https-certificate-period", "text":"https証明書有効期限は["+expire_time+"]です。URL:"+url } # API呼出し r_post = requests.post(tokenUrl,data=json.dumps(item_data),headers=headers) return r_post.json() ''' URL取得 ''' def get_httpsUrl_excel(excel_file): file_path = os.getcwd() base_path = os.path.join(file_path, 'url.xls') print(base_path) book = xlrd.open_workbook(base_path) sheet1 = book.sheets()[0] rb = xlrd.open_workbook(excel_file, formatting_info=True) wb = copy(rb) ws = wb.get_sheet(0) style = xlwt.XFStyle() # font = xlwt.Font() # font.name = '仿宋' #设置字体 # font.bold = True #字体加粗 # font.height = 320 #设置字号 # style.font = font borders = xlwt.Borders() borders.top = xlwt.Borders.THIN borders.bottom = xlwt.Borders.THIN borders.left = xlwt.Borders.THIN borders.right = xlwt.Borders.THIN style.borders = borders alignment = xlwt.Alignment() alignment.horz = xlwt.Alignment.HORZ_CENTER alignment.vert = xlwt.Alignment.VERT_CENTER style.alignment = alignment # nrows = sheet1.nrows # print('表格总行数', nrows) # ncols = sheet1.ncols # print('表格总列数', ncols) # row3_values = sheet1.row_values(2) # print('第3行值', row3_values) # col3_values = sheet1.col_values(2) # print('第3列值', col3_values) day = sheet1.cell(1, 4).value for k in range(4,sheet1.nrows): url = sheet1.cell(k, 8).value print(url) if str(url) == "" : break is_suc, expire_time, expire_days= get_expire(url) if is_suc: ws.write(k, 24, expire_days,style) ws.write(k, 29, expire_time,style) if expire_days < day: sendSlackMessage(expire_time,url) wb.save(excel_file) get_httpsUrl_excel(r"C:\httpsExpire\url.xls")
创建bat文件,可以放到Windows的任务管理器中,定时启动
python httpExpire.py exit
运行结果如下,有效日数,有效期限是取到的。