开源python脚本系列-批量查询ssl过期时间

本文涉及的产品
.cn 域名,1个 12个月
简介: 开源python脚本系列-批量查询ssl过期时间

640.png

波哥的全栈新项目pc端上线了:https://www.98dev.com有空个可以去看看,一个IT资源类导航,实时更新的哦~


脚本说明:    批量查询域名证书过期时间的脚本,这个需求应该是比较普遍的。DOMAIN_LIST用来填写域名列表,其他没什么需要特别说明的,这个是个python3的脚本脚本功能:
   

需要特别说明的是距离过期还剩这个是按照当前时间来计算,还剩多少天过期


import socket
import ssl
import datetime
# author 波哥(IT运维技术圈)
# 域名列表
DOMAIN_LIST = [
    "www.98dev.com",
    "www.baidu.com",
    "www.yahoo.com",
    "www.microsoft.com"
]
# 解析IP地址
def resolve_domain(domain):
    try:
        ips = socket.getaddrinfo(domain, None)
        return [ip[4][0] for ip in ips]
    except:
        return []
# 获取证书信息
def get_certificate_info(domain):
    context = ssl.create_default_context()
    with socket.create_connection((domain, 443)) as sock:
        with context.wrap_socket(sock, server_hostname=domain) as sslsock:
            cert = sslsock.getpeercert()
            subject = dict(x[0] for x in cert['subject'])
            issued_to = subject.get('commonName')
            issuer = dict(x[0] for x in cert['issuer'])
            issued_by = issuer.get('organizationName')
            valid_from = datetime.datetime.strptime(cert['notBefore'], '%b %d %H:%M:%S %Y %Z')
            valid_to = datetime.datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
            expire_days = (valid_to - datetime.datetime.utcnow()).days
            return (issued_to, valid_from, valid_to, expire_days, issued_by)
# 打印结果
def print_result(domain, info):
    print("="*50)
    print("Domain: {0}".format(domain))
    print("通用名: {0}".format(info[0]))
    print("生效日期: {0}".format(info[1]))
    print("到期日期: {0}".format(info[2]))
    print("距离过期还剩: {0} 天".format(info[3]))
    print("颁发机构: {0}".format(info[4]))
    ips = resolve_domain(domain)
    if ips:
        print("解析地址: {0}".format(", ".join(ips)))
    else:
        print("解析地址: N/A")
    print("="*50)
# 主程序
if __name__ == "__main__":
    for domain in DOMAIN_LIST:
        try:
            info = get_certificate_info(domain)
            print_result(domain, info)
        except:
            print("无法获取域名 {0} 的证书信息".format(domain))


执行效果如下:

640.png

别忘了给波哥一个三连!

相关文章
|
4天前
|
网络协议 Linux Python
Python脚本配置Centos静态ip地址
这是一个Python脚本,用于自动化配置CentOS系统的静态IP地址。脚本创建或修改文件,填写接口名(如ens33)、IP地址、子网掩码、网关和DNS。运行时需替换变量值并使用`sudo`以管理员权限执行。完成配置后,它会重启网络服务。注意,用户应根据实际网络环境调整参数。
Python脚本配置Centos静态ip地址
|
1天前
|
SQL API Python
`bandit`是一个Python静态代码分析工具,专注于查找常见的安全漏洞,如SQL注入、跨站脚本(XSS)等。
`bandit`是一个Python静态代码分析工具,专注于查找常见的安全漏洞,如SQL注入、跨站脚本(XSS)等。
19 8
|
1天前
|
网络协议 安全 Shell
`nmap`是一个开源的网络扫描工具,用于发现网络上的设备和服务。Python的`python-nmap`库允许我们在Python脚本中直接使用`nmap`的功能。
`nmap`是一个开源的网络扫描工具,用于发现网络上的设备和服务。Python的`python-nmap`库允许我们在Python脚本中直接使用`nmap`的功能。
23 7
|
5天前
|
数据采集 存储 监控
python 10个自动化脚本
【7月更文挑战第10天】
16 3
|
5天前
|
存储 算法 索引
1310. 子数组异或查询 异或 前缀和 python
1310. 子数组异或查询 异或 前缀和 python
|
7天前
|
Linux 网络安全 开发者
【Python】已解决:WARNING: pip is configured with locations that require TLS/SSL, however the ssl module i
【Python】已解决:WARNING: pip is configured with locations that require TLS/SSL, however the ssl module i
21 3
|
8天前
|
数据采集 数据处理 数据安全/隐私保护
Python的自动化脚本可以完成哪些任务?
【7月更文挑战第7天】Python的自动化脚本可以完成哪些任务?
19 3
|
6天前
|
运维 Python Windows
如何通过Python脚本查找并终止占用指定端口的进程
在日常的开发和运维过程中,某些端口被意外占用是一个常见的问题。这种情况可能导致服务无法启动或冲突。本文将介绍如何通过Python脚本查找并终止占用指定端口的进程,以确保系统的正常运行。
|
10天前
|
Shell 网络安全 网络虚拟化
|
1天前
|
算法 Python
我们需要一个简单的Python脚本来作为示例。假设我们有一个名为`hello_world.py`的脚本,
我们需要一个简单的Python脚本来作为示例。假设我们有一个名为`hello_world.py`的脚本,
7 0