python的证书_用 python 编写简单的证书域名到期报警脚本

将脚本放在服务器的计划任务内,定时检测,证书到期不足60天发送报警邮件及钉钉提醒。

在服务器上需要提前安装好 requests 库。 pip install requests

钉钉机器人

脚本

#!/usr/bin/env python

# -*- coding: utf-8 -*-

from datetime import datetime

from email.mime.text import MIMEText

from email.header import Header

from smtplib import SMTP

import requests

import json

host_server = 'mail.abc.com'

sender = 'lifangcheng@abc.com'

passwd = 'xxxxx'

sender_mail = 'lifangcheng@abc.com'

receiver = 'group@abc.com'

dingding_url = "https://https://oapi.dingtalk.com/robot/send?access_token=a23dd21319a3d24ff0e1839a17e0d44197abeb58858c1ed9a94484361xxxxxx"

headers = {"Content-Type": "application/json; charset=utf-8"}

server_list = [{"host": "*.open.abc.com", "expire_time": "2020-05-26"},

{"host": "*.abc.com", "expire_time": "2020-05-26"},

{"host": "*.x.abc.com", "expire_time": "2020-05-26"},

{"host": "test.abc.net", "expire_time": "2020-05-09"}]

def echo_remaining_time(server_list):

text = ""

for server in server_list:

now = datetime.now()

expire_time = datetime.strptime(server["expire_time"], "%Y-%m-%d")

diff = expire_time - now

if int(diff.days) < 60:

text = text + "证书名称: " + server["host"] \

+ " " + "剩余日期:" + str(diff.days) + "天" + "\n"

return text

text = echo_remaining_time(server_list)

post_data = {

"msgtype": "text",

"text": {

"content": text

},

"at": {

"isAtAll": True

}

}

r = requests.post(dingding_url, headers=headers,

data=json.dumps(post_data))

print r.content

def send_mail():

mail_content = echo_remaining_time(server_list)

mail_title = '重要:阿里云证书到期提醒!!!'

if mail_content:

smtp = SMTP(host_server)

# smtp.set_debuglevel(1)

smtp.ehlo(host_server)

smtp.login(sender, passwd)

msg = MIMEText(mail_content, "plain", 'utf-8')

msg["Subject"] = Header(mail_title, 'utf-8')

msg["From"] = sender_mail

msg["To"] = receiver

smtp.sendmail(sender_mail, receiver, msg.as_string())

if __name__ == '__main__':

send_mail()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值