python3 监控域名证书到期时间,并实现叮叮的及时报警

# _*_coding=utf-8 _*_
# @author 云深沾衣
# @date 2021/11/5 16:33

import re
import subprocess
from datetime import datetime
import requests


class DomainCheck:
    def __init__(self, domain_file='domain_file.txt', log_file='log.txt'):
        self.domain_file = domain_file
        self.log_file = log_file

    check_days = 5
    dingding_token = 'your_dingding_token'
    dingding_url = 'https://oapi.dingtalk.com/robot/send?access_token='

    def check_domain_ssl_status(self):
        with open(self.domain_file) as f:
            self.write_log_to_file('<------  ' + str(datetime.now()) + '    ------>' + '\n')
            for line in f:
                line = line.replace('\n', '').replace('\r', '').strip()
                if not line.startswith("#") and len(line) > 0:
                    expire_date = self.get_cert_expire_date(line)
                    content = line + ' :    ' + str(expire_date) + '\n'
                    self.write_log_to_file(content)
                    if expire_date <= self.check_days:
                        self.send_to_dingding(content)

    def send_to_dingding(self, content):
        content_data = {
            "msgtype": "text",
            "text": {
                "content": content
            }
        }
        requests.post(self.dingding_url + self.dingding_token, json=content_data)

    def write_log_to_file(self, content):
        with open(self.log_file, mode='a') as f:
            f.write(content)

    @staticmethod
    def parse_time(date_str):
        return datetime.strptime(date_str, "%b %d %H:%M:%S %Y GMT")

    @staticmethod
    def get_re_match_result(pattern, string):
        match = re.search(pattern, string)
        return match.group(1)

    def get_cert_info(self, domain):
        """获取证书信息"""
        cmd = f"curl -Ivs https://{domain} --connect-timeout 10"  # 这儿的 curl 应该使用最新版
        exitcode, output = subprocess.getstatusoutput(cmd)
        try:
            expire_date = self.get_re_match_result('expire date: (.*)', output)
        except:
            content = domain + ": This domain check failed , please check reason"
            self.send_to_dingding(content)
            expire_date = "Aug  7 00:28:51 2020 GMT"
        expire_date = self.parse_time(expire_date)
        return expire_date

    def get_cert_expire_date(self, domain):
        """获取证书剩余时间"""
        expire_date = self.get_cert_info(domain)
        # 剩余天数
        return (expire_date - datetime.now()).days


domain_check = DomainCheck('/opt/sh/text.txt', '/opt/sh/log.txt')
domain_check.check_domain_ssl_status()

修改字幕文件名字脚本

# -*- coding: utf-8 -*-
import os
import re


def getfilelist(path="", filetype=".txt"):
    filelist = []
    try:
        filenames = os.listdir(path)
        if (len(filenames) > 0):
            for fn in filenames:
                if fn.endswith(filetype):
                    fullfilename = os.path.join(path, fn)
                    filelist.append(fullfilename)
        return filelist
    except FileNotFoundError:
        print("请输入目录")


path = input("please enter your dir: ")
mkvfilelist = getfilelist(path, '.mkv')
assfilelist = getfilelist(path, '.ass')

if mkvfilelist and assfilelist:
    for i in mkvfilelist:
        for j in assfilelist:
            name = re.search(r'(S\d{2}E\d{2})', j).group(1)
            if name in i:
                newname = i.split(".", 1)[0] + ".ass"
                os.rename(j, newname)

Centos6.x/7.x下面安装特定的python版本

  • 由于openssl出现问题,在源码编译安装python3的时候,会一直不成功,我测试了几个openssl的版本最后编译的时候都不能通过,最后根据python3给出的提示,安装了LibreSSL来代替openssl,LibreSSL是在看到openssl有问题的基础上开发出来的用来替代openssl的
#!/bin/bash
WORK_DIR=`mktemp -d`

#some Dependency software
yum install gcc make wget libffi-devel unzip zlib zlib-devel bzip2 bzip2-devel ncurses ncurses-devel readline readline-devel openssl  -y
yum install openssl-devel openssl-static xz lzma xz-devel sqlite sqlite-devel gdbm gdbm-devel tk tk-devel git -y

downloadPip(){
    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
}

downloadSetuptools(){
    wget https://files.pythonhosted.org/packages/b0/d1/8acb42f391cba52e35b131e442e80deffbb8d0676b93261d761b1f0ef8fb/setuptools-40.6.2.zip
}

installLibreSSL(){
    cd $WORK_DIR && [ ! -f $WORK_DIR/libressl-2.8.1.tar.gz ] && wget https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.8.1.tar.gz
    tar zxvf libressl-2.8.1.tar.gz && cd libressl-2.8.1
    ./configure --prefix=/opt/libressl && make install
    mv /usr/bin/openssl /usr/bin/openssl.bak 
    mv /usr/include/openssl /usr/include/openssl.bak 
    ln -s /opt/libressl/bin/openssl /usr/bin/openssl 
    ln -s /opt/libressl//include/openssl /usr/include/openssl
    echo "/opt/libressl/lib" > /etc/ld.so.conf.d/libressl.conf
    ldconfig 
}

installPython2(){
    #get python2 and install python2
    cd $WORK_DIR && [ ! -f $WORK_DIR/Python-2.7.15.tar.xz ] && wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tar.xz
    xz -d Python-2.7.15.tar.xz && tar -xvf  Python-2.7.15.tar && cd Python-2.7.15
    ./configure --prefix=/opt/python2
    make  && make install  && make clean  && make distclean

    #install setuptools
    cd $WORK_DIR && [ ! -f $WORK_DIR/setuptools-40.6.2.zip ] && downloadSetuptools
    unzip setuptools-40.6.2.zip && cd setuptools-40.6.2
    /opt/python2/bin/python2 ./setup.py install

    #install pip
    cd $WORK_DIR && [ ! -f $WORK_DIR/get-pip.py ] && downloadPip
    /opt/python2/bin/python2 $WORK_DIR/get-pip.py
}

installPython3(){
    installLibreSSL
    cd $WORK_DIR && [  ! -f $WORK_DIR/Python-3.7.1.tar.xz ] && wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
    xz -d Python-3.7.1.tar.xz && tar -xvf  Python-3.7.1.tar && cd Python-3.7.1
    export LDFLAGS="-L /opt/libressl/lib"
    export CPPFLAGS="-I /opt/libressl/include"
    export PKG_CONFIG_PATH="/opt/libressl/lib/pkgconfig"
    ./configure --prefix=/opt/python3 --enable-shared  CFLAGS=-fPIC ##--enable-optimizations 是优化选项(LTO,PGO 等)加上这个 flag 编译后,性能有 10% 左右的优化,但会花更多的时间
    make  && make install  && make clean && make distclean
    echo "/opt/python3/lib" > /etc/ld.so.conf.d/python3.conf
    ldconfig
    /opt/python3/bin/pip3 install --upgrade pip
}

echo -e "\033[31m Which python version do you want to install  :  \033[0m"
select var in "Python2" "Python3" "Python2andPython3" "Exit"
do
    case $var in
        "Python2")
            installPython2
            exit
            ;;
        "Python3")
            installPython3
            exit
            ;;
        "Python2andPython3")
            installPython2
            installPython3
            exit
            ;;
        "Exit")
            exit
            ;;
        *)
            echo "Please chose 1 || 2 || 3 || 4"
            ;;
    esac
done

#clear
cd  && [ -d $WORK_DIR ] && rm $WORK_DIR -rf