1 Dnspython Processing module installation, source mode
wget http://www.dnspython.org/kits/1.9.4/dnspython-1.9.4.tar.gz
Tar zxf dnspython-1.9.4.tar.gz
CD dnspython-1.9.4/
Python setup.py Install
2 parsing examples
A record query example
#!/usr/bin/env python#-*-coding:utf-8-*-import dns.resolverdomain = raw_input ("Please enter a domain name to resolve:") A = Dns.resolver.query (Domain, ' A ') for I in A.response.answer:for j in i.items:if J.rdtype = = 1:print j.address
MX Record Example
#!/usr/bin/env python#-*-coding:utf-8-*-import dns.resolverdomain=raw_input (' Please enter a domain name: ') mx=dns.resolver.query ( Domain, ' MX ') for I in Mx:print i.preference, I.exchange
NS Record Example
Import dns.resolverdomain=raw_input (' Please enter a domain name: ') ns=dns.resolver.query (domain, ' NS ') for I in NS.response.answer:for J In I.items:print J.to_text ()
CNAME record Example
Import dns.resolverdomain=raw_input (' Please enter a domain name: ') cname=dns.resolver.query (domain, ' cname ') for I in Cname.response.answer:for J in I.items:print J.to_text ()
3 Monitoring your business with DNS domain name rotation
monitoring example
#!/usr/bin/env python# -*- coding: utf-8 -*-import dns.resolverimport Osimport httplibiplist=[]appdomain= "www.ctrip.com" Def get_iplist (domain= ""): try: a= Dns.resolver.query (Domain, ' A ') except Exception,e: print "Dns resolver error:" + STR (e) for i in A.response.answer: for j in i.items: if j.rdtype == 1: iplist.append (j.adDress) return truedef checkip (IP): checkurl=ip+ ":" getcontent= "" httplib.socket.setdefaulttimeout (5) conn=httplib. Httpconnection (Checkurl) try: conn.request ("GET", "/", headers={"Host": AppDomain}) r= Conn.getresponse () getcontent=r.read ( finally: ) print getcontent if getcontent== "<! Doctype html> ": print ip+ ": 80 port normal [ok]" else: print ip+ ": 80 port Exception [error]" if __name__== "__main__": if get_iplist (AppDomain) and len (IPList) >0: for ip in iplist: checkip (IP) else: print "Dns resolver error"
Execute Python test1.py
<! DOCTYPE html>
118.89.206.133:80 Port OK [OK]
DNS module Dnspython application and Business monitoring example