1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
'' '
192.168.0.1
--> www.xxx.com 192.168.0.2 192.168.0.3
获取业务域名A记录,查询出所有IP地址列表,在使用httplib模块的request()方法 以get方式请求监控页面,监控业务所有服务IP是否服务正常 '' '
#!/usr/bin/env python import dns.resolver import os import http.client import socket iplist=[] #定义ip列表变量 appdomain= "51cto.com" #定义业务域名(例)
def get_iplist(domain= "" ): #域名解析函数,解析成功后IP将追加到iplist
try :
A=dns.resolver.query(domain, 'A' )
except Exception as e:
print ( "dns resolver error:" +str(e))
return
for i in A.response.answer:
for j in i.items:
iplist.append(j.address)
return True
def checkip(ip): checkurl=ip + ":80"
getcontent= ""
#socket.setdefaulttimeout(5) #使用socet模块定义超时时间为5秒
http.client.socket.setdefaulttimeout(5)
conn=http.client.HTTPConnection(checkurl) #创建http链接对象
try :
conn.request( "GET" , "/" ,headers={ "HOST" :appdomain}) #发起URL请求,添加host主机t头
r=conn.getresponse()
getcontent=r.read(15) #获取URL页面前1个字符,以便可用性校验
finally:
if getcontent== "<!doctype html>" : #监控URL页的内容一般事先定义好的,比如“HTTP200”等
print (ip+ "[Ok]" )
else :
print (ip+ "[ERROR]" ) #此处可放置警告程序,可以是邮件,可以是短信
if __name__== "__main__" :
if get_iplist(appdomain) and len(iplist) > 0 : #条件:域名解析正确至少返回一个IP
for ip in iplist:
checkip(ip)
else :
print ( "DNS resolver error." )
|
本文转自 SoulMio 51CTO博客,原文链接:http://blog.51cto.com/bovin/1881256,如需转载请自行联系原作者