0x00 所需环境

server酱账号
GitHub账号
关注“方糖”微信公众号
python3

0x01 了解Server酱

「Server酱」,英文名「ServerChan」,是一款「程序员」和「服务器」之间的通信软件。其实就是从服务器推送报警和日志到手机上的一个工具。绑定微信以后就可以从Server酱服务器发送消息到微信,从而实现微信接受推送的消息日志了。
具体介绍请移步:http://sc.ftqq.com/3.version

0x02 登录,绑定账号

登入GitHub账号

访问http://sc.ftqq.com/?c=github&a=login登录GitHub账号,登入之后就会有sckey

GitHub登入产生sckey

在访问http://sc.ftqq.com/,绑定微信

绑定微信

这时候就可以发送消息推送到微信了

测试消息推送
消息接收

0x03 python代码编写

git clone https://github.com/ZTHacker/github_real_time_monitor.git

源代码如下:

#_*_ coding: utf-8 _*_
import requests
import json
import time

header = {}

def Request(api, key):
    return requests.get(api.format(key),verify=False).text


def getTotal(api, key):
    content = json.loads(Request(api, key))
    return int(content["total_count"])


def getInfo(api, key, index, send_count):
    content = json.loads(Request(api, key))
    for i in range(send_count):
        info_list[index][str(i)] = {
            "name": content["items"][i]["name"],
            "description": content["items"][i]["description"],
            "url": content["items"][i]["html_url"]
        }


# 初始化,获取初始count
def Init(api, key_words):
    for index in range(len(key_words)):
        time.sleep(20)
        key = key_words[index]
        info_list[index]["key word"] = key
        info_list[index]["flag_total"] = getTotal(api, key)


# 判断count后将信息存到列表中
def Update(api, key_words):
    for index in range(len(key_words)):
        time.sleep(20)
        key = key_words[index]
        total = getTotal(api, key)
        if info_list[index]["flag_total"] < total:
            send_count = total - info_list[index]["flag_total"]
            info_list[index]["flag_total"] = total # update
            getInfo(api, key, index, send_count)


# 发送到Server酱
def Send(info_list, key):
    url = 'https://sc.ftqq.com/{}.send'.format(key)
    for i in range(len(info_list)):
        title = "Github监控更新提醒:{}".format(info_list[i]["key word"])
        desp = ""
        if "name" in str(info_list[i]):
            count = len(info_list[i]) - 2
            for k in range(count):
                desp += "名称:{}\n\n简介:{}\n\n地址:{}\n\n".format(info_list[i][str(k)]["name"], info_list[i][str(k)]["description"], info_list[i][str(k)]["url"])
                del(info_list[i][str(k)])

            data = {
                "text": title,
                "desp": desp
            }
            res = requests.post(url, data=data,verify=False)



if __name__ == '__main__':
    print("initial...                " + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

    api = 'https://api.github.com/search/repositories?q={}&sort=updated'

    # set your config
    config = {
        "key_words": ['CVE-2020', 'password'],      #监控的关键词,可多个监控
        "intval": 100,  # 发送间隔, 单位为s;
        "SCKEY": 'you key',  # Server酱 KEY
    }

    info_list = []
    for i in range(len(config["key_words"])):   
        info_list.append({})

    Init(api, config["key_words"])
    
    while 1:
        try:
            print("waiting to update...      " + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
            time.sleep(config["intval"])
            Update(api, config["key_words"])
            print(info_list) # 控制台打印信息
            Send(info_list, config["SCKEY"]) # 推送信息到微信
        except:
            print("Connection refused by the server..")
            print("Let me sleep for 5 seconds")
            print("ZZZzzz...")
            time.sleep(5)
            print("Was a nice slepp ,now let me continue...")
            continue

在config处填入要监控的关键词个sckey值

配置关键词和sckey

然后使用python3运行,这里运行的话可以放在自己的服务器上使用screen或者nohup python3 github_tool.py > log.txt 2>&1 &

运行完以后就是等待了,脚本中也可以自己设置推送的时间间隔

接受关键词监控信息

0x04 END

GitHub :https://github.com/ZTHacker/github_real_time_monitor,欢迎点🌟!

发表评论