本文共 5654 字,大约阅读时间需要 18 分钟。
DNS处理模块dnspython
下载地址dnspython提供一个DNS解析器类——resolver,使用它的query方法来实现域名的查询功能。
query(self,qname,rdtype=1,rdclass=1,tcp=False,source=None,raise_on_no_answer=True,source_prot=0) 其中,qname参数为查询的域名,rdtype参数用来指定RR资源的类型,常用的类型如下: A记录:将主机名转换成IP地址 MX记录:邮件交换记录,定义邮件服务器的域名 CNAME记录:指别名记录,实现域名间的映射。 NS记录:标记区域的域名服务器及授权子域 PTR记录:反向解析,与A记录相反,将地址换成主机名 SOA记录:SOA标记,一个起始授权区的定义。rdclass参数用于指定网络类型,可选的值为IN,CH,HS,IN为默认,使用最广泛。
tcp参数用于指定查询是否启用TCP协议,默认不启用。 source和source_prot指定查询源地址和端口,默认值为查询设备IP地址和0。 raise_on_no_answer指定当查询无应答时是否触发异常,默认为TRUE。3.1 - 实现A记录查询方法
response.answer方法获取查询回应信息[python]
<embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">
import dns.resolver
def main():
domain = raw_input('Enter an domain:')
A = dns.resolver.query(domain,'A') #指定查询类型为A记录
for i in A.response.answer: #通过response.answer方法获取查询回应信息
for j in i.items:
print j
if name=='main':
main()
[root@lsn-linux python27]# python
Enter an . 119.75.217.56 119.75.218.773.2 - 实现MX记录查询方法
[python]
<embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">
import dns.resolver
def main():
domain = raw_input('Enter an domain:')
MX = dns.resolver.query(domain,'MX') #指定查询类型为MX记录
for i in MX:
print'MX preference=',i.preference,' mail exchanger=',i.exchange
if name=='main':
main()
[root@lsn-linux python]# python
Enter an domain:139.com MX preference= 5 mail exchanger= mx1.mail.139.com. MX preference= 10 mail exchanger= mx2.mail.139.com. MX preference= 20 mail exchanger= mx3.mail.139.com.3.3 - 实现NS记录查询方法
[python]
<embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">
import dns.resolver
def main():
domain = raw_input('Enter an domain:')
NS = dns.resolver.query(domain,'NS') #指定查询类型为NS记录
for i in NS.response.answer: #通过response.answer方法获取查询回应信息
for j in i.items:
print j
if name=='main':
main()
[root@lsn-linux python]# python
Enter an domain:baidu.com ns4.baidu.com. ns2.baidu.com. dns.baidu.com. ns7.baidu.com. ns3.baidu.com.3.4 - 实现CNAME记录查询方法
[python]
<embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">
import dns.resolver
def main():
domain = raw_input('Enter an domain:')
CNAME = dns.resolver.query(domain,'CNAME') #指定查询类型为CNAME记录
for i in CNAME.response.answer: #通过response.answer方法获取查询回应信息
for j in i.items:
print j
if name=='main':
main()
[root@lsn-linux python]# python
Enter an . [root@lsn-linux python]# python Enter an3.5 - DNS域名轮询业务监控
步骤: a:实现域名的解析,获取域名所有A记录解析IP列表 b:对IP列表进行HTTP级别的探测[python]
<embed id="ZeroClipboardMovie_5" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&width=16&height=16" wmode="transparent" style="box-sizing: border-box;">
import dns.resolver
import os
import httplib
iplist=[]
appdomain=""
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)
return True
def checkip(ip):
checkurl=str(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]"
def main():
if get_iplist(appdomain) and len(iplist)>0:
for ip in iplist:
checkip(ip)
else:
print "dns resolver error."
<embed src="" flashvars="clipboard=%23!%2Fusr%2Fbin%2Fpython%0A%23__%20coding%3Autf-8%20__%0A%23Filename%" width="0" height="0" type="application/x-shockwave-flash" style="box-sizing: border-box;">
if name=='main':
main()