本文共 4257 字,大约阅读时间需要 14 分钟。
环境说明:linux系统 python 2.6.6
安装:
1 2 3 4 5 6 7 | wget https://pypi.python.org/packages/b3/e3/091c6489f0b573b8a4069ce956d037061ae9321401c89323386fe748dc9f/dnspython-1.12.0.zip unzip dnspython-1.12.0.zip cd dnspython-1.12.0 python setup.py install |
导入模块是:
1 | import dns.resolver |
dnsPython模块提供了大量的dns处理方法,最常见的是域名查询,dnsPython提供一个DNS解析类—>resolver,使用它的query方法来实现域名查询功能,query方法定义如下:
1 | query( self ,qname,rdtype = 1 ,rdclass = 1 ,tcp = False ,source = None ,raise_on_no_answer = True ,source_port = 0 ) |
其中:qname参数为查询的域名。rdtype参数用来指定RR的资源的类型,常用的有以下几种
A记录 | 将主机名转化为IP。 |
MX记录 | 邮件交换记录,定义邮件服务器域名。 |
CHAME记录 | 指别名记录实现域名间的映射。 |
NS记录 | 标记区域的域名服务器以及授权子域。 |
PTR记录 | 反向解析,与A记录相反,将IP转化成主机名。 |
SOA记录 | SOA标记,一个起始授权区定义。 |
(一)实现A记录例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/env python #-*- coding:utf-8 -*- # 2017,8,24 import dns.resolver domain = raw_input ( 'Please input an domain:' ) A = dns.resolver.query(domain, 'A' ) for i in A.response.answer: for j in i.items: print j.address |
执行结果:以QQ为例子:
1 2 3 4 5 | [root @China dns] # python simple1.py Please input an domain:www.qq.com 61.135.157.156 125.39.240.113 [root @China dns] # |
(二),实现MX记录的例子:
1 2 3 4 5 6 7 8 9 10 11 12 | #!/usr/bin/env python #-*- coding:utf-8 -*- # 2017,8,24 import dns.resolver domain = raw_input ( 'Please input an domain:' ) MX = dns.resolver.query(domain, 'MX' ) for i in MX: print 'MX preferce=' ,i.preference, 'mail exchaner=' ,i.exchange |
以QQ和163为例看结果:
1 2 3 4 5 6 7 8 9 10 11 12 | [root@China dns] # python simple2.py Please input an domain:163.com MX preferce= 10 mail exchaner= 163mx01.mxmail.netease.com. MX preferce= 50 mail exchaner= 163mx00.mxmail.netease.com. MX preferce= 10 mail exchaner= 163mx03.mxmail.netease.com. MX preferce= 10 mail exchaner= 163mx02.mxmail.netease.com. [ root@China dns] # python simple2.py Please input an domain:qq.com MX preferce= 30 mail exchaner= mx1.qq.com. MX preferce= 20 mail exchaner= mx2.qq.com. MX preferce= 10 mail exchaner= mx3.qq.com. |
(三)NS记录例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/env python #-*- coding:utf-8 -*- # 2017,8,24 import dns.resolver domain = raw_input ( 'Please input an domain:' ) ns = dns.resolver.query(domain, 'NS' ) for i in ns.response.answer: for j in i.items: print j.to_text() |
以QQ和百度为例子进行验证:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@China dns] # python simple3.py Please input an domain:baidu.com ns7.baidu.com. ns2.baidu.com. ns4.baidu.com. dns.baidu.com. ns3.baidu.com. [root@China dns] # python simple3.py Please input an domain:qq.com ns3.qq.com. ns1.qq.com. ns4.qq.com. ns2.qq.com. |
(四)CNAME例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/env python #-*- coding:utf-8 -*- # 2017,8,24 import dns.resolver domain = raw_input ( 'Please input an domain:' ) cname = dns.resolver.query(domain, 'CNAME' ) for i in cname.response.answer: for j in i.items: print j.to_text() |
应用实例:DNS域名轮循业务监控(参考资料:http://book.51cto.com/art/201411/456723.htm)
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 46 47 48 49 50 51 | #!/usr/bin/env python #-*- coding:utf-8 -*- #2017,8,25 #change dns is ok? import dns.resolver import os import httplib import requests iplist = [] #定义域名IP列表变量 appdomain = "www.qq.com" #定义业务域名 def get_iplist(domain = ""): #域名解析函数,解析成功IP将被追加到iplist 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) #追加到iplist return True def checkip(ip): checkurl = ip + ":80" getcontent = "" httplib.socket.setdefaulttimeout( 5 ) #定义连接超时时间为5秒 conn = httplib.HTTPConnection(checkurl) #创建http连接对象 try : conn.request( "GET" , "/" ,headers = { "Host" :appdomain}) #发起url请求,添加host头 r = conn.getresponse() getcontent = r.status #获取URL页面状态码 finally : if getcontent = = 200 : 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" |
执行结果:
1 2 3 4 5 6 7 8 9 | [root@China dns] # python simple5.py 125.39.240.113[ok] 61.135.157.156[ok] [root@China dns] # python simple5.py 61.135.157.156[ok] 125.39.240.113[ok] [root@China dns] # python simple5.py 125.39.240.113[ok] 61.135.157.156[ok] |
本文转自 天道酬勤VIP 51CTO博客,原文链接:http://blog.51cto.com/tdcqvip/1958996
转载地址:http://hcmix.baihongyu.com/