{"id":741,"date":"2021-11-11T15:33:54","date_gmt":"2021-11-11T07:33:54","guid":{"rendered":"https:\/\/www.aiqinqin.info\/?p=741"},"modified":"2022-07-13T17:21:19","modified_gmt":"2022-07-13T09:21:19","slug":"python3-%e7%9b%91%e6%8e%a7%e5%9f%9f%e5%90%8d%e8%af%81%e4%b9%a6%e5%88%b0%e6%9c%9f%e6%97%b6%e9%97%b4%ef%bc%8c%e5%b9%b6%e5%ae%9e%e7%8e%b0%e5%8f%ae%e5%8f%ae%e7%9a%84%e5%8f%8a%e6%97%b6%e6%8a%a5%e8%ad%a6","status":"publish","type":"post","link":"https:\/\/www.aiqinqin.info\/?p=741","title":{"rendered":"python3 \u76d1\u63a7\u57df\u540d\u8bc1\u4e66\u5230\u671f\u65f6\u95f4\uff0c\u5e76\u5b9e\u73b0\u53ee\u53ee\u7684\u53ca\u65f6\u62a5\u8b66"},"content":{"rendered":"<pre><code class=\"language-python \"># _*_coding=utf-8 _*_\n# @author \u4e91\u6df1\u6cbe\u8863\n# @date 2021\/11\/5 16:33\n\nimport re\nimport subprocess\nfrom datetime import datetime\nimport requests\n\n\nclass DomainCheck:\n    def __init__(self, domain_file='domain_file.txt', log_file='log.txt'):\n        self.domain_file = domain_file\n        self.log_file = log_file\n\n    check_days = 5\n    dingding_token = 'your_dingding_token'\n    dingding_url = 'https:\/\/oapi.dingtalk.com\/robot\/send?access_token='\n\n    def check_domain_ssl_status(self):\n        with open(self.domain_file) as f:\n            self.write_log_to_file('&lt;------  ' + str(datetime.now()) + '    ------&gt;' + '\\n')\n            for line in f:\n                line = line.replace('\\n', '').replace('\\r', '').strip()\n                if not line.startswith(\"#\") and len(line) &gt; 0:\n                    expire_date = self.get_cert_expire_date(line)\n                    content = line + ' :    ' + str(expire_date) + '\\n'\n                    self.write_log_to_file(content)\n                    if expire_date &lt;= self.check_days:\n                        self.send_to_dingding(content)\n\n    def send_to_dingding(self, content):\n        content_data = {\n            \"msgtype\": \"text\",\n            \"text\": {\n                \"content\": content\n            }\n        }\n        requests.post(self.dingding_url + self.dingding_token, json=content_data)\n\n    def write_log_to_file(self, content):\n        with open(self.log_file, mode='a') as f:\n            f.write(content)\n\n    @staticmethod\n    def parse_time(date_str):\n        return datetime.strptime(date_str, \"%b %d %H:%M:%S %Y GMT\")\n\n    @staticmethod\n    def get_re_match_result(pattern, string):\n        match = re.search(pattern, string)\n        return match.group(1)\n\n    def get_cert_info(self, domain):\n        \"\"\"\u83b7\u53d6\u8bc1\u4e66\u4fe1\u606f\"\"\"\n        cmd = f\"curl -Ivs https:\/\/{domain} --connect-timeout 10\"  # \u8fd9\u513f\u7684 curl \u5e94\u8be5\u4f7f\u7528\u6700\u65b0\u7248\n        exitcode, output = subprocess.getstatusoutput(cmd)\n        try:\n            expire_date = self.get_re_match_result('expire date: (.*)', output)\n        except:\n            content = domain + \": This domain check failed , please check reason\"\n            self.send_to_dingding(content)\n            expire_date = \"Aug  7 00:28:51 2020 GMT\"\n        expire_date = self.parse_time(expire_date)\n        return expire_date\n\n    def get_cert_expire_date(self, domain):\n        \"\"\"\u83b7\u53d6\u8bc1\u4e66\u5269\u4f59\u65f6\u95f4\"\"\"\n        expire_date = self.get_cert_info(domain)\n        # \u5269\u4f59\u5929\u6570\n        return (expire_date - datetime.now()).days\n\n\ndomain_check = DomainCheck('\/opt\/sh\/text.txt', '\/opt\/sh\/log.txt')\ndomain_check.check_domain_ssl_status()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p># _*_coding=utf-8 _*_ # @author \u4e91\u6df1\u6cbe\u8863 # @date 2021\/11\/5  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,5],"tags":[],"class_list":["post-741","post","type-post","status-publish","format-standard","hentry","category-python","category-program"],"_links":{"self":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=741"}],"version-history":[{"count":5,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":799,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/741\/revisions\/799"}],"wp:attachment":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}