Last active
November 18, 2018 14:41
-
-
Save wdc63/869c104ac43841f20b25a40b4dd3b7d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pip install requests | |
import subprocess, platform | |
import requests | |
import time | |
def ping(host): | |
""" | |
Returns True if host responds to a ping request | |
""" | |
# Ping parameters as function of OS | |
ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1" | |
args = "ping " + " " + ping_str + " " + host | |
need_sh = False if platform.system().lower()=="windows" else True | |
# Ping | |
return subprocess.call(args, shell=need_sh) == 0 | |
on_state = 1 | |
off_state = 1 | |
def watch(host): | |
global on_state, off_state | |
while True: | |
get_ping = ping(host) | |
if get_ping: | |
##如果检测到在线,且离线状态为真(off_state>0),发送上线信号,并将离线状态重置为假(=0) | |
if off_state > 0: | |
off_state = 0 | |
requests.get("https://sc.ftqq.com/[SCKEY(登入后可见)].send?text=服务器上线!") | |
else: | |
on_state += 1 | |
else: | |
##如果检测到连续离线五次,且在线状态为真(off_state>0),发送离线信号,并将在线状态重置为假(=0) | |
if on_state > 0 and off_state >= 5: | |
on_state = 0 | |
requests.get("https://sc.ftqq.com/[SCKEY(登入后可见)].send?text=服务器离线!") | |
else: | |
off_state += 1 | |
time.sleep(10) | |
watch('www.baidu.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment