跳到内容
🛠️ToolsShed

证书到期计算器

计算SSL/TLS证书到期前的天数并跟踪多个证书。

关于此工具

SSL和TLS证书对于保护网站和应用程序至关重要,但它们会在特定日期过期。证书过期计算器可以帮助您跟踪证书的过期时间,让您提前收到通知,以便在证书失效导致服务中断之前进行续期。该工具对于需要管理基础设施中多个证书的系统管理员、DevOps工程师和网站所有者至关重要。

要使用计算器,只需输入证书的过期日期,该工具将立即显示剩余天数、确切的日期和清晰的警告状态。您可以通过逐个输入每个证书来跟踪多个证书,从而轻松制定续期计划。该工具完全在浏览器中运行,因此您的证书信息永远不会离开您的设备。

证书过期是导致网站、API和电子邮件服务器服务中断的常见原因。通过使用此计算器主动监控您的证书,您可以提前计划续期并避免代价高昂的停机时间。该工具适用于任何管理SSL/TLS基础设施的人员,从拥有单个证书的小型企业到拥有数百个证书的大型企业。

常见问题

代码实现

import ssl
import socket
from datetime import datetime, timezone

def get_cert_expiry(hostname: str, port: int = 443) -> dict:
    """Fetch TLS certificate expiry info for a hostname."""
    context = ssl.create_default_context()
    with socket.create_connection((hostname, port), timeout=5) as sock:
        with context.wrap_socket(sock, server_hostname=hostname) as ssock:
            cert = ssock.getpeercert()

    not_after = datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
    not_after = not_after.replace(tzinfo=timezone.utc)
    now = datetime.now(timezone.utc)
    days_remaining = (not_after - now).days

    return {
        "hostname": hostname,
        "expires": not_after.strftime('%Y-%m-%d'),
        "days_remaining": days_remaining,
        "status": "expired" if days_remaining < 0
                  else "critical" if days_remaining < 14
                  else "warning" if days_remaining < 30
                  else "good",
        "subject": dict(x[0] for x in cert.get('subject', [])),
        "issuer": dict(x[0] for x in cert.get('issuer', [])),
    }

# Example
result = get_cert_expiry("example.com")
print(f"Expires: {result['expires']}")
print(f"Days remaining: {result['days_remaining']}")
print(f"Status: {result['status']}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.