DNS Processing Module Dnspython
Dnspython is a python-implemented DNS toolkit that supports almost all record types and can be used to query, transmit, and dynamically update zone information while supporting TSIG (transactional signature) authentication messages and EDNS0 (extended DNS).
In System management, we can use its query function to achieve DNS service monitoring and validation of the results, can replace the tools such as Nslookup and dig, easy to integrate with the existing platform.
Download Dnspython Module
[Email protected] ~]# python3-m easy_install-i Http://pypi.douban.com/simple/dnspython
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 records, defining the domain name of the mail server |
| CNAME record |
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 an IP address to a host name |
| SOA Records |
SOA markup, a definition of a starting authorization area |
Examples of common parsing types:
A record (implement a record query method source code):
[[email protected] pkg1]# vim dnsa.py
#!/usr/bin/python3#import dns.resolverdomain = input (' Please input an domain: ') #输入域名地址A = dns.resolver.query (domain, ' A ') #指定查询类型为A记录for i in a.response.answer: # Get query Response information for j in i.items: #遍历回应信息 through the Response.answer method print (j.address)
[Email protected] pkg1]# Python3 dnsa.py please input an domain:www.google.com93.46.8.89
MX record (Implement MX record Query method source code):
[Email protected] pkg1]# vim mxdns.py
#!/usr/bin/python3#import dns.resolverdomain = input (' Please input an domain: ') #输入域名地址MX = dns.resolver.query (domain, ' MX ') #指定查询类型为MX记录for i in MX: #通过response. Answer method to get query response information print (' MX preference = ', i.preference, ' main exchanger = ', i.exchange)
[[email protected] pkg1]# python3 mxdns.py please input an domain: baidu.comMX preference = 20 main exchanger = mx50.baidu.com.MX preference = 10 main exchanger = mx.n.shifen.com.mx preference = 20 main exchanger = mx1.baidu.com.mx preference = 20 main Exchanger = jpmx.baidu.com.
NS record (realize NS record Query method source code):
[[email protected] pkg1]# vim nsdns.py
#!/usr/bin/python3#import dns.resolverdomain = input (' Please input an domain: ') #输入域名地址ns = dns.resolver.query (domain, ' NS ') #指定查询类型为NS记录for i in ns.response.answer: #通过response. Answer method to get query response information for j in i.items: #遍历回应信息 print (j.to_ Text ())
[Email protected] pkg1]# Python3 nsdns.py please input an domain:qq.comns3.qq.com.ns4.qq.com.ns1.qq.com.ns2.qq.com.
CNAME record (Implement CNAME record Query method source code):
[Email protected] pkg1]# vim cnamedns.py
#!/usr/bin/python3#import Dns.resolverdomain = input (' Please input a domain: ') #输入域名地址cname = Dns.resolver.query ( Domain, ' CNAME ') #指定查询类型为CNAME记录for i in Ns.response.answer: #通过response. Answer method Gets the query response information for the J in I.items: #遍历回应信息 Print (J.to_text ())
This article is from the "Shei" blog, make sure to keep this source http://kurolz.blog.51cto.com/11433546/1935039
Python Learning note-dns processing module Dnspython