python3批量探测远程主机端口是否开放并钉钉报警
本脚本环境:python3.6.6 安装 requests
#!/usr/bin/python3 # -*- coding: utf-8 -*- __author__ = 'laojia' __time__ = '2020/03/12 14:57' from socket import * import requests import datetime import time import json import sys import os headers = {'Content-Type': 'application/json;charset=utf-8'} api_url = "----your dingding url----" #发送钉钉 def dingding(title,mess): json_text= { "msgtype": "text", "at": { "atMobiles": [ "---phone num---" ], "isAtAll": False }, "text": { "content": "[{}]:\n{}".format(title,mess) } } requests.post(api_url,json.dumps(json_text),headers=headers) #端口检测 def portScanner(host,port): try: s = socket(AF_INET,SOCK_STREAM) s.connect((host,port)) mess="主机 {} 端口 {} open".format(host,port) write_log(mess) print(mess) s.close() except: mess="主机 {} 端口 {} closed".format(host,port) write_log(mess) dingding("---title---",mess) print(mess) #写检测日志 def write_log(mess): with open('D:\\checkport\\checkport.log', 'a+') as f: f.write("time: {} mess: {}\n".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),mess )) #主函数 def main(): while(True): for port in range(8085,8086): setdefaulttimeout(5) portScanner('---ip----',port) time.sleep(60) # 每隔 60 秒检测一次 if __name__ == '__main__': main()
0顶
0 踩
共 0 条评论