Dnspython Module
1. Module Domain Name resolution method
A record, hostname converted to IP address
MX record, mail exchange record, define the domain name of the mail server
CNAME records, alias Records, implementing mappings between domain names
NS records, domain name servers for labeled zones, and authorization subdomains
PTR records, reverse resolution, and a record instead, convert IP to host name
SOA record, SOA tag, definition of a starting authorization area
2. Examples of common parsing types description
Implementing a Query method for a record
Import Dns.resolver
Domain = raw_input (' Please input a domain: ')
A = dns.resolver.query (domain, ' A ')
For I in A.response.answer:
For J in I.items:
Print J.address
220.181.111.86
123.125.114.144
220.181.111.85
MX record
Import Dns.resolver
Domain = raw_input (' Please input a domain: ')
mx = dns.resolver.query (domain, ' mx ')
For I in MX:
print ' MX preference = ', i.preference, ' mail exchanger = ', I.exchange
MX preference = mail exchanger = jpmx.baidu.com.
MX preference = mail exchanger = mx50.baidu.com.
MX preference = Ten mail exchanger = mx.n.shifen.com.
MX preference = mail exchanger = mx1.baidu.com.
NS Records
Import Dns.resolver
Domain = raw_input (' Please input a domain: ')
NS = dns.resolver.query (domain, ' NS ')
For I in Ns.response.answer:
For J in I.items:
Print J.to_text ()
Ns4.baidu.com.
Dns.baidu.com.
Ns7.baidu.com.
Ns2.baidu.com.
Ns3.baidu.com.
CNAME record
Import Dns.resolver
Domain = raw_input (' Please input a domain: ')
CNAME = dns.resolver.query (domain, ' cname ')
For I in Cname.response.answer:
For J in I.items:
Print J.to_text ()
2.DNS domain name polling business monitoring
Most DNS resolution is a domain name corresponding to an IP, but through the DNS polling technology can be a domain name corresponding to multiple IP, so that the simplest and efficient load balancing, but the biggest disadvantage of this scenario is that the target host is not automatically rejected, so good business host service monitoring is critical. This example analyzes the resolution IP of the domain name, and then combines the service port detection to realize automatic monitoring, when adding and removing IP in the domain name resolution, no need to modify the monitoring script.
(1) Steps
To achieve the resolution of the domain name, get the domain name of all A records resolution IP list
HTTP-level probing for IP lists
#!/usr/bin/env pythonimport dns.resolverimport osimport httplib iplist = [] appdomain = ' 163.com ' 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: &nbSp; iplist.append (j.address) return true def 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 (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."
This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1582388
Python dnspython module operation and maintenance automation