• 周五. 3月 29th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

监测主机存活python小脚本

admin

11月 28, 2021

监测主机存活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)
记得旧文章 便是新举子

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注