Infi-chu:
http://www.cnblogs.com/Infi-chu/
Module: Dnspython
Function:
- Support for all record types
- can be used to query, transmit and dynamically update zone information
- Support for Tsig (transactional signature) authentication messages and EDNS0 (extended DNS)
Installation:
wget HTTP://WWW.DNSPYTHON.ORG/KITS/1.9.4/DNSPYTHON-1.9.4.TAR.GZTAR-ZXVF DNSPYTHON-1.9.4.TAR.GZCD Dnspython-1.9.4python setup.py Install
Module domain Name resolution method:
Dnspython provides a DNS parser class--resolver, using the query () method to implement the lookup function of the domain name
The Query () method uses:
#import Dns.resolver#query (Self,qname,rdtype=1,rdclass=1,tcp=false,source=none,raise_on_no_answer=true,source_ port=0)
QName the domain name for the query.
Rdtype is used to specify the type of RR resource, which is commonly used in the following ways:
- A record, converting the hostname to an IP address
- MX record, mail exchange record, to 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
The rdclass is used to specify the network type, and the optional values are:
- In, default is in
- Ch
- Hs
TCP is used to specify whether the query enables the TCP protocol
Source and Source_port Specify the query origin address and port, default query device IP and 0
The raise_on_no_answer is used to specify whether an exception is triggered when the query has no answer, and the default is True
A record Query method:
#import dns.resolver#dom=raw_input (' Input domain address: ') #A =dns.resolver.query (DOM, ' a ') # Specifies a record of query type a #for a in a.response.answer : # Get query Response information by Response.answer method # for B in A.items: # Traverse Response message # Print (b.address)
MX Record Query method:
#import dns.resolver#dom=raw_input (' Input domain address: ') #MX =dns.resolver.query (DOM, ' MX ') #for A in mx:# print (' MX priority: ', A.preference, ' mail exchanger: ', A.exchange)
NS Record Query method:
#import dns.resolver#dom=raw_input (' Enter the first-level domain address: ') #NS =dns.resolver.query (DOM, ' NS ') #for A in ns.respinse.answer:# for B In a.items:# print (B.to_text ())
CNAME record Query method:
#import dns.resolver#dom=raw_input (' Input domain address: ') #cname =dns.resolver.query (DOM, ' cname ') #for A in Cname.response.answer : # for B in a.items:# print (B.to_text ())
DNS domain name polling business monitoring:
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
Realize:
#import dns.resolver#import Os#import httplib#ip_list=[] # defines the IP list of the domain name #dom=raw_input (' Input domain name: ') #def get_ip_list (dom= '): # try:# A=dns.resolver.query (DOM, ' A ') #解析A记录类型 # except exception,e:# print (' DNS parse error: ', str (e)) # return# for A in A.respons e.answer:# for B in a.items:# ip_list.append (b.address) # Add to IP list #def checkip (IP): # checkurl=ip+ ': ' # getcontent= ' # HTT Plib.socket.setdefaulttimeout (10) # defines the HTTP connection timeout time, 10s# conn=httplib. Httpconnection (checkurl) # Create HTTP Connection object # try:# conn.request (' GET ', '/', headers={' HOST ':d om}) # initiate URL request, add host host Header # r= Conn.getresponse () # Getcontent=r.read (15) # Gets the 15 characters before the URL page to make the availability check # finally:# if getcontent== ' <!doctype html> ' : # The content of the Monitoring URL page is generally pre-defined # print (ip+ ' [OK] ') # else:# print (ip+ ' [ERROR] ') #if __name__= "__main__": # If Get_ip_list (DOM) and Len (ip_list) >0: #域名解析正确, and the IP list has data # for a in ip_list:# print (a) # else:# print (' DNS parsing error ')
Python automated operation and maintenance--dns processing module