python 检测域名状态 发表于 2022-01-10 浏览量 454 没有评论 # 脚本描述 利用curl获取网站状态码,并将状态码发送至飞书,来达到监控网站状态。 ``` #!/usr/bin/python import json import sys import os import time import requests def send_message(color,content): title = 'website_status' robot_id = 'xxx' # 飞书获取机器人ID webhook = "https://open.feishu.cn/open-apis/bot/v2/hook/%s" % robot_id header = { "Content-Type": "application/json", "Charset": "UTF-8" } post_data = { "msg_type": "interactive", "card": { "config": { "wide_screen_mode": True, "enable_forward": True }, "elements": [ { "tag": "div", "text": { "content": content, "tag": "lark_md" }, } ], "header": { "title": { "content": title, "tag": "plain_text" }, "template": color } } } post_data_json = json.dumps(post_data) info = requests.post(url=webhook, data=post_data_json, headers=header) print(info) send_message('Green','测试') ``` # 域名检测,并发送到飞书 ``` def main(): with open('gw_domain.txt','r') as f: domain_data = f.readlines() domain_list = [] for item in domain_data: domain_list.append(item.strip()) for one in domain_list: cmd = "curl -I -m 15 -o /dev/null -s -w %{http_code} " + one status = os.popen(cmd) ss = int(status.read()) if ss == 200: print("域名:{{{0}}} http_code ==> {1}".format(one,ss)) content = ("域名:{{{0}}} http_code ==> {1}".format(one,ss)) color = 'Green' # send_message(color,content) else: print("域名:{{{0}}} http_code ==> {1} Please check!!!".format ( one,ss )) content = ("域名:{{{0}}} 网站状态码 ==> {1} 请检查!!!".format ( one,ss )) color = 'Red' send_message(color,content) if __name__ == "__main__": main() ``` - 有问题的发送至飞书,没问题的,直接记录到日志中。 # 创建计划任务 ``` # 每周一执行一次备案域名网站状态 00 10 * * 1 /usr/bin/python3 /data/sh/website_status.py >> /data/logs/crontab/website_status.log 2>&1 ```