检测域名证书有效期

检测域名有效期

#coding:utf-8

import urllib2
import requests
import re

import threading
import datetime

class Check_domain(object):

	def __init__(self,url):
		self.url=url
		self.domain_url={}

	def check_domain(self,bb):
		header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
		'Host':'whois.chinaz.com',
		'Referer':'http://whois.chinaz.com/%s'%bb,
		'Upgrade-Insecure-Requests':'1'
		}

		params={'domain':bb,'isforceupdate':'1','ws':''}
		page_info=requests.get(self.url,headers=header,params=params)
		# print page_info.url
		result=page_info.content
		reg = re.compile(r'<div class="fl WhLeList-left">过期时间</div><div class="fr WhLeList-right"><span>(.*?)</span>')
		time_list=re.findall(reg,result)
		if len(time_list):
			end_time = datetime.datetime.strptime(str(time_list[0]),"%Y年%m月%d日")
			time_list[0]=datetime.datetime.strftime(end_time,"%Y-%m-%d")
			start_time =datetime.datetime.now()
			# print (end_time-start_time).days
			self.domain_url[bb]={'deadline':time_list[0],'dead_days':(end_time-start_time).days}

if __name__ == '__main__':
	thread=[]
	check=Check_domain('http://whois.chinaz.com')
	check_domain=['www.baidu.com','dding.cn','www.gmial.com']
	for url in check_domain:
		t= threading.Thread(target=check.check_domain,args=(url,))
		t.start()
		thread.append(t)
	for t in thread:
		t.join()
	for s,v in check.domain_url.items():
		print s,v

发送邮箱

def send_email(self,content, mailto, get_sub ):
		mail_host = 'smtp.163.com' 
		mail_user = '****@163.com'
		mail_pwd = '*******'
		msg = MIMEText( content.encode('utf8'), _subtype = 'html', _charset = 'utf8')  
		msg['From'] = mail_user
		msg['Subject'] = u'%s' %get_sub
		msg['To'] = ",".join(mailto)
		try:
			print 'connecting ',mail_host  
			smb=smtplib.SMTP(mail_host)
			smb.login(mail_user, mail_pwd)
			print 'send email'  
			smb.sendmail(mail_user, ",".join(mailto), msg.as_string())  
			print 'close the connection between the mail server'  
			smb.close()
		except Exception as e:  
			print 'Exception: ', e

test.sh

#!/usr/bin/bash
host=$1
port=443
end_date=`openssl s_client -host $host -port $port -showcerts </dev/null 2>/dev/null |
	sed -n '/BEGIN CERTIFICATE/,/END CERT/p' |
      openssl x509 -text 2>/dev/null |
      sed -n 's/ *Not After : *//p'`
if [ -n "$end_date" ]
then
    end_date_seconds=`date '+%s' --date "$end_date"`
# date指令format字符串时间。
    now_seconds=`date '+%s'`
    echo "($end_date_seconds-$now_seconds)/24/3600" | bc
fi

检测证书有效期

#!/usr/bin/env python
#coding:utf-8
import os
import smtplib  
from email.mime.text import MIMEText  
class Check_https(object):
	def __init__(self,to_list,check_list):
		for url in check_list:
		     
		    result = os.popen('sh test.sh {0}'.format(url))
		    result = result.read().strip().split('\n')[-1]
		    print result
		    if result.isdigit():
			    if int(result)>0:
			        print "沒有過期"
			    else:
				print "國了"
			        self.send_email(result,to_list,'hi 过期了')

	def send_email(self,content, mailto, get_sub ):
		mail_host = 'smtp.163.com'  
		mail_user = '*****@163.com'  
		mail_pwd = '***********'
		mail_postfix="163.com"
		msg = MIMEText(content.encode('utf8'),_subtype='plain', _charset = 'utf8')  
		msg['From'] = mail_user  
		msg['Subject'] = u'%s' %get_sub  
		msg['To'] = ",".join(mailto)  
		try:  
			print 'connecting ',mail_host  
			smb=smtplib.SMTP()
			smb.connect(mail_host)
			smb.login(mail_user, mail_pwd)
			print 'send email'  
			smb.sendmail(mail_user, mailto, msg.as_string())  

			print 'close the connection between the mail server'  
			smb.close()  
		except Exception as e:  
			print 'Exception: ', e  

if __name__ == '__main__':
    to_list = ['*****@163.com'] 
    check_list = ['www.baidu.com','dding.cn','www.gmail.com']
    check = Check_https(to_list,check_list)

posted @ 2017-05-13 20:47  xiaoganghu  阅读(1344)  评论(0编辑  收藏  举报