python 用于检查给定主机是否在线的脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用于检查给定主机是否在线的脚本相关的知识,希望对你有一定的参考价值。

'''
Author: Vikas Yadav

Description:
Script to check if a given host is online or not
This will not work if the given host is blocking ping requests.
'''
import os
import re
import time

# Takes IP of the target as an argument 
# Returns true if the given IP is online
# False otherwise.
def online(IP):
	temp = os.popen("ping -c 1 -W 5 %s" %(IP)).read()
	print temp
	result = re.search("Unreachable", temp)
	if result:
		return False
	else:
		return True


while 1:
	IP = "Enter IP Here"
	if online(IP):
		os.system('notify-send "Target is online" --urgency=critical')
	time.sleep(10)

以上是关于python 用于检查给定主机是否在线的脚本的主要内容,如果未能解决你的问题,请参考以下文章