import socket
import os,time
import configparser
import requests as rq
config=configparser.RawConfigParser()
def local():
strtime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
return strtime
class tomcat():
def __init__(self):
while True:
config.read("config.ini", encoding="utf-8-sig")
self.sections = config.sections()
self.sections.remove('time')
self.sections.remove('robot')
self.time=int(config['time']['time'])
self.access_token=config['robot']['access_token']
print(self.sections)
self.run()
def isconnet(self,ip,port):
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sk.connect((ip, port))
return True
except Exception as e:
return False
def chongqi(self,path,judge):
if judge:
try:
panfu=path.split("\\")[0]
os.system(r"{0} && cd {1}\bin && startup.bat ".format(panfu,path))
except Exception as e:
print(e)
def run(self):
print(local())
for i in self.sections:
ipport = config[i]['ip']
ip=ipport.split(':')[0]
port = int(ipport.split(':')[1])
root = config[i]['root']
message = config[i]['message']
isboot = config[i]['isreboot']
print(ipport, root, message, bool(int(isboot)))
if self.isconnet(ip,port):
print("连接成功")
else:
print("连接失败")
self.url = "https://oapi.dingtalk.com/robot/send?access_token={}".format(self.access_token)
js = {"msgtype": "text", "text": {"content": message}, "at": {"atMobiles": [], "isAtAll": True}}
try:
rq.post(self.url, json=js)
except Exception as e:
print(e)
print("机器人发送消息异常")
self.chongqi(root, bool(int(isboot)))
print("发送信息"+message)
time.sleep(self.time*60)
t=tomcat()
配置文件
[time]
#单位分钟,间隔时间
time=1
[robot]
#配置机器人
access_token=
[case1]
ip = 127.0.0.1:8080
#tomcat目录
root=C:/tomcat
#发送消息文本
message=
#1重启 0 不重启
isreboot=1
[case2]
ip =
root=
message=
isreboot=