Python實現(xiàn)檢測SSL證書是否過期
之前寫了個 shell 版本的 SSL 證書過期巡檢腳本 (文章:《SSL 證書過期巡檢腳本》),后臺反響還是很不錯的
那么今天咸魚給大家介紹一下 python 版本的 SSL 證書過期巡檢腳本 (完整代碼在文末)
思路
導(dǎo)入相關(guān)模塊
import ssl import socket import time from datetime import datetime
首先我們創(chuàng)建一個 domain.txt 用來存放要檢查的域名和對應(yīng)的 IP 地址
www.baidu.com:180.101.50.242,180.101.50.188 www.bing.com:202.89.233.101,202.89.233.100
我們讀取該文件,把里面的域名和對應(yīng)的每個 ip 取出來,并存放到字典 domains 里面
domains = {}
with open('domain.txt', 'r', encoding='utf-8') as file:
for line in file:
domain, ip_pool = line.strip().split(':')
domains[domain] = ip_pool.split(',')取出來之后我們循環(huán)遍歷字典,去獲取每個域名對應(yīng)的證書信息(ssl_connect 函數(shù))
def ssl_connect(domain, ip):
# 設(shè)置socket的超時時間為5秒
socket.setdefaulttimeout(5)
# 創(chuàng)建默認的SSL上下文
context = ssl.create_default_context()
# 創(chuàng)建一個SSL套接字
skt = context.wrap_socket(socket.socket(), server_hostname=domain)
try:
# 建立SSL連接
skt.connect((ip, 443))
# 獲取證書過期時間
end_date = skt.getpeercert()['notAfter'].strip(' GMT')
# 創(chuàng)建一個字典,存儲本次連接中的域名、IP 地址和證書過期時間信息
skt_info = {'domain': domain, 'ip': ip, 'end_date': end_date}
except ssl.CertificateError as e:
cert = e
except socket.timeout:
cert = 'Connect refused'
except ConnectionResetError as e:
cert = 'Connect reset' + str(e)
except socket.gaierror as e:
cert = 'Connnect gaierror'
finally:
# 關(guān)閉SSL套接字
skt.close()
return skt_infossl_connect 函數(shù)返回一個字典 skt_info,包含當前連接的域名、ip 地址和證書過期時間
# skt_info 內(nèi)容
{'domain': 'www.baidu.com', 'ip': '180.101.50.242', 'end_date': 'Aug 6 01:51:05 2024'}
{'domain': 'www.baidu.com', 'ip': '180.101.50.188', 'end_date': 'Aug 6 01:51:05 2024'}
{'domain': 'www.bing.com', 'ip': '202.89.233.101', 'end_date': 'Aug 16 03:47:45 2023'}
{'domain': 'www.bing.com', 'ip': '202.89.233.100', 'end_date': 'Aug 16 03:47:45 2023'}然后我們調(diào)用 check_cert_time 函數(shù)進行證書有效期檢查和提示
info = [ssl_connect(domain, ip) for domain, ip_pool in domains.items() for ip in ip_pool] [check_cert_time(i) for i in info]
check_cert_time 函數(shù)內(nèi)容如下:
def check_cert_time(info):
# 獲取當前時間戳
current_timestamp = int(time.time())
# 將證書過期時間轉(zhuǎn)換成時間戳
date_object = datetime.strptime(info['end_date'], "%b %d %H:%M:%S %Y")
end_timestamp = int(date_object.timestamp())
# 計算剩余天數(shù)
remain_day = (end_timestamp - current_timestamp) / 86400
# 打印域名、IP 地址和證書過期時間信息
print(f"域名:{info['domain']},ip 地址:{info['ip']},證書過期時間:{info['end_date']}")
# 根據(jù)剩余天數(shù)進行不同的提示
# 如果證書過期時間減去當前時間的天數(shù)小于七天的話,則提示需要準備更換證書了
if 0 < remain_day < 7:
print('剩余時間小于七天!請及時更換證書!')
elif remain_day < 0:
print('證書已過期!請及時更換證書!')
else:
print(f"剩余天數(shù)為:{remain_day:.2f}天\n")最后我們執(zhí)行一下代碼,看看結(jié)果如何

完整代碼
import ssl
import socket
import time
from datetime import datetime
def ssl_connect(domain, ip):
# 設(shè)置socket的超時時間為5秒
socket.setdefaulttimeout(5)
# 創(chuàng)建默認的SSL上下文
context = ssl.create_default_context()
# 創(chuàng)建一個SSL套接字
skt = context.wrap_socket(socket.socket(), server_hostname=domain)
try:
# 建立SSL連接
skt.connect((ip, 443))
# 獲取證書過期時間
end_date = skt.getpeercert()['notAfter'].strip(' GMT')
# 創(chuàng)建一個字典,存儲本次連接中的域名、IP 地址和證書過期時間信息
skt_info = {'domain': domain, 'ip': ip, 'end_date': end_date}
except ssl.CertificateError as e:
cert = e
except socket.timeout:
cert = 'Connect refused'
except ConnectionResetError as e:
cert = 'Connect reset' + str(e)
except socket.gaierror as e:
cert = 'Connnect gaierror'
finally:
# 關(guān)閉SSL套接字
skt.close()
return skt_info
def check_cert_time(info):
# 獲取當前時間戳
current_timestamp = int(time.time())
# 將證書過期時間轉(zhuǎn)換成時間戳
date_object = datetime.strptime(info['end_date'], "%b %d %H:%M:%S %Y")
end_timestamp = int(date_object.timestamp())
# 計算剩余天數(shù)
remain_day = (end_timestamp - current_timestamp) / 86400
# 打印域名、IP 地址和證書過期時間信息
print(f"域名:{info['domain']},ip 地址:{info['ip']},證書過期時間:{info['end_date']}")
# 根據(jù)剩余天數(shù)進行不同的提示
# 如果證書過期時間減去當前時間的天數(shù)小于七天的話,則提示需要準備更換證書了
if 0 < remain_day < 7:
print('剩余時間小于七天!請及時更換證書!')
elif remain_day < 0:
print('證書已過期!請及時更換證書!')
else:
print(f"剩余天數(shù)為:{remain_day:.2f}天\n")
if __name__ == "__main__":
domains = {}
with open('domain.txt', 'r', encoding='utf-8') as file:
for line in file:
domain, ip_pool = line.strip().split(':')
domains[domain] = ip_pool.split(',')
info = [ssl_connect(domain, ip) for domain, ip_pool in domains.items() for ip in ip_pool]
[check_cert_time(i) for i in info]到此這篇關(guān)于Python實現(xiàn)檢測SSL證書是否過期的文章就介紹到這了,更多相關(guān)Python檢測SSL是否過期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用PyQt的QLabel組件實現(xiàn)選定目標框功能的方法示例
這篇文章主要介紹了使用PyQt的QLabel組件實現(xiàn)選定目標框功能的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
python用requests實現(xiàn)http請求代碼實例
這篇文章主要介紹了python用requests實現(xiàn)http請求過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
Python+wxPython實現(xiàn)文件名批量處理
在日常的文件管理中,我們經(jīng)常需要對文件進行批量處理以符合特定的命名規(guī)則或需求,本文主要介紹了如何使用wxPython進行文件夾中文件名的批量處理,需要的可以參考下2024-04-04
使用wxpython實現(xiàn)的一個簡單圖片瀏覽器實例
這篇文章主要介紹了使用wxpython實現(xiàn)的一個簡單圖片瀏覽器實例,根據(jù)自己的特殊需求而寫,需要的朋友可以參考下2014-07-07
Python使用Qt5實現(xiàn)水平導(dǎo)航欄的示例代碼
本文主要介紹了Python使用Qt5實現(xiàn)水平導(dǎo)航欄的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python操作Excel文件的11種方法(全網(wǎng)最全)
在日常工作或開發(fā)過程中,Excel文件作為一種常用的數(shù)據(jù)存儲格式,其高效便捷的數(shù)據(jù)處理能力被廣泛應(yīng)用于數(shù)據(jù)統(tǒng)計、數(shù)據(jù)分析等領(lǐng)域,Python作為一種強大的編程語言,提供了豐富的庫支持來實現(xiàn)對Excel文件的操作,本篇將詳細介紹如何使用Python來操作Excel文件2025-03-03

