Python automated operation and maintenance--dns processing module

Source: Internet
Author: User
Tags parse error mail exchange mx record

Infi-chu:

http://www.cnblogs.com/Infi-chu/

Module: Dnspython

Function:

    1. Support for all record types
    2. can be used to query, transmit and dynamically update zone information
    3. 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:

    1. A record, converting the hostname to an IP address
    2. MX record, mail exchange record, to define the domain name of the mail server
    3. CNAME records, alias Records, implementing mappings between domain names
    4. NS records, domain name servers for labeled zones, and authorization subdomains
    5. PTR records, reverse resolution, and a record instead, convert IP to host name
    6. SOA record, SOA tag, definition of a starting authorization area

The rdclass is used to specify the network type, and the optional values are:

    1. In, default is in
    2. Ch
    3. 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:

    1. To achieve the resolution of the domain name, get the domain name of all A records resolution IP list
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.