运维脚本——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()

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

F——

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值