Python DNS Processing Module Dnspython
The following is a list of common methods used in the Dnspython module, drawing on the book, "Python Automated operations: Technology and best Practices"
Dnspython provides a large number of DNS processing methods, the most commonly used is the domain name query. Dnspython provides a DNS parsing class resolver, which uses its query method to implement the querying 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 record: Mail exchange record that defines the domain name of the mail server
CNAME records: Alias records for mapping between domain names
NS records: Domain name servers and authorization subdomains for labeled zones
PTR record: Reverse parsing, in contrast to a record, converts IP address to host name
SOA record: SOA tag, definition of a starting authorization area
Rdclass specified network type, IN,CH,HS, in default
TCP Specifies whether the query enables the TCP protocol (not enabled by default)
Source and Source_port Specify the address and port of the query source, which defaults to the IP address of the query device and 0
RAISE_ON_NO_ANSWER Specifies whether to trigger an exception when the query does not answer, by default True
Here is the code
#! /usr/bin/env python
#--Coding:utf-8-*-**
Import Dns.resolver
Domain = raw_input (' Please input a domain: ')
Class DNS:
def get_a (self,domain): # query A record
Print (' Output a record ')
Try
A = Dns.resolver.query (Domain, ' A ')
For I in A.response.answer:
For J in I.items:
Print (j.address)
Except Exception as E:
Print (' Not queried to a record ')
def get_mx (Self,domain): # Querying MX Records
Print (' Output MX record ')
Try
mx = dns.resolver.query (Domain, ' MX ')
For I in MX:
print ' MX preference = ', I.preference, ' Mail exchanger= ', I.exchange
Except Exception as E:
Print (' not queried to MX record ')
def Get_ns (Self,domain): # Querying NS Records
Print (' Output NS record ')
Try
NS = Dns.resolver.query (Domain, ' NS ')
For I in Ns.response.answer:
For J in I.items:
Print J.to_text ()
Except Exception as E:
Print (' not queried to NS record ')
def get_cname (Self,domain): # Querying CNAME Records
Print (' Output CNAME record ')
Try
CNAME = Dns.resolver.query (Domain, ' cname ')
For I in Cname.response.answer:
For J in I.items:
Print J.to_text ()
Except Exception as E:
Print (' Not queried to CNAME ')
If name = = 'main':
D = DNS ()
D.get_a (Domain)
Print ('----------------')
D.GET_MX (Domain)
Print ('----------------')
D.get_ns (Domain)
Print ('----------------')
D.get_cname (Domain)
Print ('----------------')
Python DNS Processing Module Dnspython