Dnspython is a Python-implemented DNS toolkit that supports almost all record types.
can be used to query, transmit, and dynamically update zone information, while supporting Tsig (transactional signature) authentication messages and EDNS0 (extended DNS).
In the aspect of system management, we can use its query function to realize DNS service monitoring and verifying the results of the resolution.
First, the module domain name resolution method
Dnspython module provides a large number of DNS processing methods, the most common method is the domain name query.
Dnspython provides a DNS parser class--resolver that uses its query method to implement the querying function of the domain name. The query method is defined as follows:
Query (self,qname,rdtype=1,rdclass=1,tcp=false,source=none,raise_on_no_answer=true,source_port=0)
Where the QName parameter is the domain name of the query. The Rdtype parameter is used to specify the type of RR resource, which is commonly used in the following ways:
(1) A record: Convert host name to IP address;
(2) MX record: Mail exchange record, define the mail server domain name;
(3) CNAME record: Alias record, realize the mapping between domain names;
(4) NS record: The authorized server and authorization subdomain of the marked area;
(5) PTR record: Reverse parsing, in contrast to the a record, the IP is converted to the host name;
(6) SOA record: SOA tag, the definition of a starting authorization area;
The Rdclass parameter is used to specify the network type, with an optional value of in, CH, and HS, where in is the default and most widely used.
The TCP parameter is used to specify whether the query enables the TCP protocol, and the default is False (not enabled).
Source with the Source_port parameter as the specified query for the origin address and port, the default value is query device IP and 0.
The Raise_on_no_answer parameter specifies whether to trigger an exception when the query has no answer, which is true by default.
Ii. Description of common analytic type examples
Common types of DNS resolution include A, MX, NS, CNAME, and so on.
These DNS types of queries can be implemented simply by using the Dns.resolver.query method of the Dnspython, which provides a source of data for the functionality to be implemented later.
1.A Records
#!/usr/bin/env Python2#Coding:utf-8ImportDns.resolverdomain= Raw_input ('Please enter the domain name:') A= Dns.resolver.query (Domain,'A')#Specify query type forIinchA.response.answer:#Get query Information forJinchI.items:#Traversal Response Information PrintJ.address
Execution Result:
[Email protected] ~]# python2/home/test/dnspython/simple.py
Please enter the domain name: www.google.com
74.86.17.48
2.MX Records
#!/usr/bin/env Python2#Coding:utf-8ImportDns.resolverdomain= Raw_input ('Please enter the domain name:') MX= Dns.resolver.query (Domain,'MX') forIinchMX:Print 'MX preference=', I.preference,'Mail exchanger=', I.exchange
Execution Result:
[[Email protected] ~] # python2/home/test/dnspython/simple2.py Please enter the domain name: 163. commx Preference= ten mail exchanger= 163mx01.mxmail.netease.com.mx Preference= ten mail exchanger= 163mx02.mxmail.netease.com.mx Preference= The Mail exchanger= 163mx00.mxmail.netease.com.mx Preference= ten mail exchanger= 163mx03.mxmail.netease.com.
3.NS Records
# !/usr/bin/env Python2 # Coding:utf-8 Import = raw_input (' Please enter domain name:'= dns.resolver.query (domain,'NS ') ') for in ns.response.answer: for inch I.items: print J.to_text ()
Execution Result:
[[Email protected] ~] # python2/home/test/dnspython/simple3.py Please enter the domain name: baidu.comdns.baidu.com.ns2.baidu.com.ns7.baidu.com.ns3.baidu.com.ns4.baidu.com.
4.CNAME Records
[[email protected] Dnspython]#Cat simple4.py#!/usr/bin/env Python2#Coding:utf-8ImportDns.resolverdomain= Raw_input ('Please enter the domain name:') CNAME= Dns.resolver.query (Domain,'CNAME') forIinchCname.response.answer: forJinchI.items:PrintJ.to_text ()
DNS Processing Module Dnspython