0
点赞
收藏
分享

微信扫一扫

检查ssl证书过期时间

笙烛 2023-05-05 阅读 69

通过python脚本实现

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

import re
import time
import subprocess
from datetime import datetime
from io import StringIO
import requests,json
def send_msg(data, phone='18358570770'):
     webhook = "https://oapi.dingtalk.com/robot/send?access_token=174ae8946096e738dcc910c8cebb20f198ac1a8050c6d050268731a62a76a403"
     header = {
      "Content-Type": "application/json",
     }
     data = {
      "msgtype": "text",
      "text": {
       "content": data
      },
      "at": {
       "atMobiles": [
        phone,
       ],
       "isAtAll": False
      }
 }

 r = requests.post(url=webhook, headers=header, data=json.dumps(data))
def main(domain):
    f = StringIO()
    comm = "curl -Ivs https://%s --connect-timeout 10"%domain

    result = subprocess.getstatusoutput(comm)
    f.write(result[1])
    print(f.getvalue())
    m = re.search('start date: (.*?)\n.*?expire date: (.*?)\n.*?common name: (.*?)\n.*?issuer: CN=(.*?)\n', f.getvalue(), re.S)
    if m:
        start_date = m.group(1)
        expire_date = m.group(2)
        common_name = m.group(3)
        issuer = m.group(4)


    # time 字符串转时间数组
    if start_date:
        start_date = start_date.replace("月",'')
    if expire_date:
        expire_date = expire_date.replace("月",'')
    start_date = time.strptime(start_date, "%m %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, "%m %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(remaining)
    msg="""
    title:到期提醒
    域名:{}
    通用名:{}
    开始时间:{}
    到期时间:{}
    剩余时间:{}
    颁发机构:{}
    """.format(domain,common_name,start_date_st,expire_date_st,remaining,issuer)
    if remaining<7:
        send_msg(msg)
    print ('域名:', domain)
    print ('通用名:', common_name)
    print ('开始时间:', start_date_st)
    print ('到期时间:', expire_date_st)
    print ('剩余时间: %d天'%remaining)
    print ('颁发机构:', issuer)
    print ('*'*30)

    time.sleep(0.5)

if __name__ == "__main__":
    domains = ['cz.xwx.app.d2ty.com']
    for domain in domains:
        main(domain)


举报

相关推荐

0 条评论