我们如何从 dnspython 获取 TXT、CNAME 和 SOA 记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13842116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do we get TXT, CNAME and SOA records from dnspython?
提问by Shabareesh
I have a requirement to have a dns query function to query a server for various records. I figured out how to get the MX record (most of the examples show this), A record and NS record. How do I get the TXT, CNAME and SOA records?
我需要一个 dns 查询功能来查询服务器的各种记录。我想出了如何获取 MX 记录(大多数示例都展示了这一点)、A 记录和 NS 记录。如何获取 TXT、CNAME 和 SOA 记录?
Sample code snippet:
示例代码片段:
import dns.resolver
answer=dns.resolver.query("google.com", "A")
for data in answer:
print data.address
I tried replacing the query type with TXT and the data.address object with data.text, data.data etc, but ended up with attribute errors. What are the references for the data types I mentioned earlier?
我尝试用 TXT 替换查询类型,用 data.text、data.data 等替换 data.address 对象,但最终出现了属性错误。我之前提到的数据类型的引用是什么?
回答by Lars Nordin
(To answer how you can figure out the returned data)
(回答如何找出返回的数据)
You can get the TXT, CNAME, and SOA records a similar way but you just have to get the correct attributes depending on the DNS response object.
您可以通过类似的方式获取 TXT、CNAME 和 SOA 记录,但您只需要根据 DNS 响应对象获取正确的属性。
Using the python dir() built-in is your friend and one way to figure out what attributes exist in the DNS response object - handy when API documentation is not available.
使用内置的 python dir() 是您的朋友,也是找出 DNS 响应对象中存在哪些属性的一种方法 - 在 API 文档不可用时很方便。
To figure out the appropriate attributes, change your for loop temporarily to the following:
要找出适当的属性,请将您的 for 循环暂时更改为以下内容:
for data in answer:
print dir(data)
print data
Another and quicker way is to look at the API documentation for dnspython, these pages list the attributes for each returned object.
另一种更快的方法是查看 dnspython 的 API 文档,这些页面列出了每个返回对象的属性。
Lastly, you could look at the source if the library is in python or if not, then if the C code is available.
最后,如果库在 python 中,或者如果不是,则可以查看源代码,然后查看 C 代码是否可用。
(And to answer your question:)
(并回答你的问题:)
Here are examples of TXT, CNAME and SOA queries:
以下是 TXT、CNAME 和 SOA 查询的示例:
TXT
文本
answers = dns.resolver.query('google.com', 'TXT')
print ' query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
for txt_string in rdata.strings:
print ' TXT:', txt_string
CNAME
名称
http://www.dnspython.org/docs/1.15.0/dns.rdtypes.ANY.CNAME.CNAME-class.html
http://www.dnspython.org/docs/1.15.0/dns.rdtypes.ANY.CNAME.CNAME-class.html
answers = dns.resolver.query('mail.google.com', 'CNAME')
print ' query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
print ' cname target address:', rdata.target
SOA
SOA
http://www.dnspython.org/docs/1.15.0/dns.rdtypes.ANY.SOA.SOA-class.html#section-InstanceVariables
http://www.dnspython.org/docs/1.15.0/dns.rdtypes.ANY.SOA.SOA-class.html#section-InstanceVariables
answers = dns.resolver.query('google.com', 'SOA')
print 'query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
print ' serial: %s tech: %s' % (rdata.serial, rdata.rname)
print ' refresh: %s retry: %s' % (rdata.refresh, rdata.retry)
print ' expire: %s minimum: %s' % (rdata.expire, rdata.minimum)
print ' mname: %s' % (rdata.mname)
回答by Meny Issakov
You can try something a bit different.
你可以尝试一些不同的东西。
Instead of querying each time per record type, you can make a single query for ANY record. This way if that domain has both TXT, CNAME etc... you'll get one object with all the data.
您可以对任何记录进行单个查询,而不是每次查询每个记录类型。这样,如果该域同时具有 TXT、CNAME 等...您将获得一个包含所有数据的对象。
from dns.resolver import dns
name_server = '8.8.8.8' #Google's DNS server
ADDITIONAL_RDCLASS = 65535
request = dns.message.make_query('google.com', dns.rdatatype.ANY)
request.flags |= dns.flags.AD
request.find_rrset(request.additional, dns.name.root, ADDITIONAL_RDCLASS,
dns.rdatatype.OPT, create=True, force_unique=True)
response = dns.query.udp(request, name_server)
Hope this might assist you.
希望这可以帮助你。
回答by FabricioFCarv
Exampling with a previous answer, create the dnsdig.py file with:
以之前的答案为例,使用以下内容创建 dnsdig.py 文件:
import sys
import socket
import dns.resolver
print 'Argument List:', str(sys.argv)
site = sys.argv[1]
dns_server = sys.argv[2]
# Basic CNAME query the host's DNS
for rdata in dns.resolver.query(site, 'CNAME') :
print rdata.target
# Basic A query the host's DNS
for rdata in dns.resolver.query(site, 'A') :
print rdata.address
# Setting an specific DNS Server
resolver = dns.resolver.Resolver()
resolver.nameservers = [socket.gethostbyname(dns_server)]
# Basic CNAME query with the specific DNS server
answer = resolver.query(site, 'CNAME');
for rdata in answer :
print rdata.target
# Basic A query with the specific DNS server
answer = resolver.query(site, 'A');
for rdata in answer :
print rdata.address
To run:
跑步:
python dnsdig.py www.youtube.com 8.8.8.8

