Dnspython is a DNS toolkit implemented in Python that can be used to query, transmit, and dynamically update zone information, support Tsig (transaction signature) authentication messages, and EDNS0 (extended DNS). In System management, we can use the query function to implement the DNS service monitoring and the resolution of the results.
Installation of Dnspython modules
wget http://www.dnspython.org/kits/1.9.4/dnspython-1.9.4.tar.gz
TAR-ZXVF dnspython-1.9.4.tar.gz
CD dnspython-1.9.4
Python setup.py Install
Dnspython provides a large number of DNS processing methods, the most common is the domain name query. Dnspython provides a DNS resolution class resolver, which uses its query method to implement the query function of the domain name.
Query (self, QName, Rdtype = 1, Rdclass = 1, TCP = False, Source = None, Raise_on_no_answer = True, Source_port = 0)
The QName parameter is the domain name of the query, and the Rdtype parameter is used to specify the RR resource.
A record: Convert host name to IP address
MX Records: Mail exchange records, defining the domain name of the mail server
CNAME records: Alias Records, to achieve the mapping between the domain name
NS records: Domain name servers and authorized subdomains for branded zones
PTR records: Reverse parsing, converting IP addresses to host names, contrary to a record
SOA record: SOA markup, a definition of a starting authorization area
Rdclass Specify network type, IN,CH,HS, in default
TCP Specifies whether the query enables the TCP protocol (default is not enabled)
Source and Source_port Specify the address and port of the query sources, default to the IP address of the query device, and 0
RAISE_ON_NO_ANSWER Specifies whether an exception is triggered when a query is not answered, and defaults to True
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
`
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
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 ()
Enter only one level domain name
CNAME Records
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 ()