当前位置: 首页 > news >正文

运维脚本——8.证书自动化管理

场景:自动化SSL/TLS证书的申请、续期和部署,避免证书过期导致服务中断。
示例:使用Shell脚本配合Let's Encrypt的Certbot工具自动续期证书。

#!/bin/bash
# 自动续期Let's Encrypt证书并重启服务
certbot renew --quiet --post-hook "systemctl reload nginx"

扩展案例:检查证书过期时间并触发告警。

import ssl
import socket
import datetime
import smtplib

def check_cert_expiry(domain, port=443):
    context = ssl.create_default_context()
    with socket.create_connection((domain, port)) as sock:
        with context.wrap_socket(sock, server_hostname=domain) as ssock:
            cert = ssock.getpeercert()
    expires_on = datetime.datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
    days_left = (expires_on - datetime.datetime.now()).days
    return days_left

domain = "example.com"
threshold = 30
days_left = check_cert_expiry(domain)

if days_left < threshold:
    msg = f"证书 {domain} 将在 {days_left} 天后过期!"
    smtp_server = smtplib.SMTP('smtp.example.com', 587)
    smtp_server.login('admin@example.com', 'password')
    smtp_server.sendmail('admin@example.com', 'ops-team@example.com', msg)
    smtp_server.quit()

http://www.dtcms.com/a/31768.html

相关文章:

  • 使用 Three.js 实现流光特效
  • Hutool - Log:自动识别日志实现的日志门面
  • 解释性语言与编译性语言
  • 解决 Vue.js 中使用 vue-print-nb 打印一页的问题
  • 现代企业软件测试人员需求与发展方向深度解析
  • 独立开发者之SEO基础:dofollow和 nofollow
  • 【部署优化十五】【深度揭秘《DeepSeek安全审计:OWASP Top10防护方案》】
  • Redis基础学习
  • 对计算机中缓存的理解和使用Redis作为缓存
  • dockerfile 使用环境变量
  • 【Java高级篇】——第16篇:高性能Java应用优化与调优
  • 当 OpenAI 不再 open,DeepSeek 如何掀起 AI 开源革命?
  • 装箱和拆箱是什么?(C#)
  • 关于Java 反射的简单易懂的介绍
  • 一文熟练掌握Spring Framework
  • 以初学者视角探索智能体学习之旅
  • 新书上线 |《零门槛AIGC应用实战——Serverless+AI 轻松玩转高频AIGC场景》免费下载
  • upload-labs靶场
  • Spring Boot中整合Flink CDC 数据库变更监听器来实现对MySQL数据库
  • 【Python爬虫(48)】分布式爬虫:解锁多领域数据宝藏的密码
  • java Web
  • 宠物智能可穿戴产品调研报告
  • Microsoft Office 2024 软件安装教程(免费)
  • 前端八股——Vue3系列
  • Codeforces 559C 详细题解
  • 深入解析C++函数指针与指针函数:从原理到实战
  • 无法打开包括文件: “crtdbg.h”: No such file or directory
  • 【Blender】二、建模篇--06,曲线建模/父子级和蒙皮修改器
  • 《CentOS 7 镜像源失效终极解决方案(2024年更新)》——生命周期终止后的镜像修复与替代方案
  • Linux编程----filelo(2)