大部分的DNS解析都是一個域名對應一個IP地址,但是通過DNS輪循技術可以做到一個域名對應多個IP,從而實現(xiàn)最簡單且高效的負載平衡啤覆,不過此方案最大的弊端是目標主機不可用時無法被自動剔除.
[root@bogon dns]# cat SJ.py
!/usr/bin/python
import dns.resolver
import os
import httplib
iplist=[]
appdomain=raw_input("please input Monitoring of the domain name:")
def get_iplist(domain=""):
try:
A = dns.resolver.query(domain, 'A')
except Exception,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=""
httplib.socket.setdefaulttimeout(5)
conn=httplib.HTTPConnection(checkurl)
try:
conn.request("GET", "/",headers = {"Host": appdomain})
r=conn.getresponse()
getcontent =r.read(15)
finally:
if getcontent=="<!doctype html>":
print ip+" [OK]"
else:
print ip+" [Error]"
if name=="main":
if get_iplist(appdomain) and len(iplist)>0:
for ip in iplist:
checkip(ip)
else:
print "dns resolver error."
測試:
輸入你自己的域名,隨便輸入一個會報錯
[root@bogon dns]# python SJ.py
please input Monitoring of the domain name:www.51auto.com
203.80.144.183 [Error]
[root@bogon dns]#