监测主机存活python小脚本

监测主机存活python小脚本

@(coding)[python,笔记]

此脚本用于监测主机存活性。原理:通过向目标主机发送icmp数据包测试,通过python循环和定时任务来完成,很简单。IP替换为监测主机ip,3是设置的时间,每三秒监测一次。

# --*-- coding:utf-8 --*--
import os
from datetime import datetime
import time

os.system('chcp 65001')    #解决cmd下出现乱码问题
 
def timer(t=5):
    while True:
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
        status=os.system("ping -n 1 IP")
        if status == 0 :
        	print ("alive"*2)
        else:
        	print ("dead"*8)
        	str1 = "The host is HALT at "+datetime.now().strftime("%Y-%m-%d %H:%M:%S")+'status'
        	fp=open("dead_Time.txt",a)
        	fp.write(str1)
        	fp.close()
        time.sleep(t)

timer(3)
posted @ 2021-08-22 23:30  壹個人坐在角落  阅读(252)  评论(0编辑  收藏  举报