1、实现域名的解析,获取域名所有的A记录解析IP列表

2、对IP列表进行HTTP级别的探测。

#!/usr/bin/python

import dns.resolver

import os

import httplib


iplist=[]    #定义域名IP列表变量

appdomain="www.baidu.com"    #定义业务域名


def get_iplist(domain=""):    #域名解析函数,解析成功IP将追加到iplist

    try:

        A = dns.resolver.query(domain, 'A')    #解析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)    #追加到iplist

    return True


def checkip(ip):

    checkurl=ip+":80"

    getcontent=""

    httplib.socket.setdefaulttimeout(5)    #定义http连接超时时间(5秒)

    conn=httplib.HTTPConnection(checkurl)    #创建http连接对象


    try:

        conn.request("GET", "/",headers = {"Host": appdomain})  #发起URL请求,添加host主机头

        r=conn.getresponse()

        getcontent =r.read(15)   #获取URL页面前15个字符,以便做可用性校验

    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."



附:python中if __name__ == '__main__': 的解析


  当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用.

  模块是对象,并且所有的模块都有一个内置属性 __name__。一个模块的 __name__ 的值取决于您如何应用模块。如果 import 一个模块,那么模块__name__ 的值通常为模块文件名,不带路径或者文件扩展名。但是您也可以像一个标准的程序样直接运行模块,在这 种情况下, __name__ 的值将是一个特别缺省"__main__"。

[root@localhost dnspython]#cat test.py 

#!/usr/bin/python

import os

print "os: "+os.__name__

print "    "+__name__

~                     

[root@localhost dnspython]# python test.py 

os: os

    __main__