溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于python檢查SSL證書到期情況代碼實例

發(fā)布時間:2021-03-11 09:53:04 來源:億速云 閱讀:260 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)基于python檢查SSL證書到期情況代碼實例,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

代碼示例:

# coding: utf-8 
# 查詢域名證書到期情況

import re
import time
import subprocess
from datetime import datetime
from io import StringIO

def main(domain):
  f = StringIO()
  comm = f"curl -Ivs https://{domain} --connect-timeout 10"

  result = subprocess.getstatusoutput(comm)
  f.write(result[1])

  m = re.search('start date: (.*?)\n.*?expire date: (.*?)\n.*?common name: (.*?)\n.*?issuer: CN=(.*?)\n', f.getvalue(), re.S)
  start_date = m.group(1)
  expire_date = m.group(2)
  common_name = m.group(3)
  issuer = m.group(4)

  # time 字符串轉(zhuǎn)時間數(shù)組
  start_date = time.strptime(start_date, "%b %d %H:%M:%S %Y GMT")
  start_date_st = time.strftime("%Y-%m-%d %H:%M:%S", start_date)
  # datetime 字符串轉(zhuǎn)時間數(shù)組
  expire_date = datetime.strptime(expire_date, "%b %d %H:%M:%S %Y GMT")
  expire_date_st = datetime.strftime(expire_date,"%Y-%m-%d %H:%M:%S")

  # 剩余天數(shù)
  remaining = (expire_date-datetime.now()).days

  print ('域名:', domain)
  print ('通用名:', common_name)
  print ('開始時間:', start_date_st)
  print ('到期時間:', expire_date_st)
  print (f'剩余時間: {remaining}天')
  print ('頒發(fā)機構(gòu):', issuer)
  print ('*'*30)

  time.sleep(0.5)

if __name__ == "__main__":
  domains = ['www.baidu.com'] 
  for domain in domains:
    main(domain)

結(jié)果示例:

域名: www.baidu.com
通用名: baidu.com
開始時間: 2019-05-09 01:22:02
到期時間: 2020-06-25 05:31:02
剩余時間: 82天
頒發(fā)機構(gòu): GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
******************************

關(guān)于“基于python檢查SSL證書到期情況代碼實例”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI