I am learning how to use the python dns object. Quick question as I see many examples show methods of using the dns.resolver method with the DNS record type(CNAME, NS, etc). Is there a way to use this dns object to query a DNS name and pull it's resolution with the record type. Similar to what DIG supplies in the answer section.
Thanks,
Jim
You can get the type with rdatatype
>>> import dns.resolver
>>> answer = dns.resolver.query('google.com')
>>> rdt = dns.rdatatype.to_text(answer.rdtype)
>>> print(rdt)
A
Here's an example of a CNAME:
>>> cname = dns.resolver.query("mail.unixy.net", 'CNAME')
>>> for i in cname.response.answer:
... for j in i.items:
... print j.to_text()
...
unixy.net.
TXT:
>>> txt = dns.resolver.query("unixy.net", 'TXT')
>>> for i in txt.response.answer:
... for j in i.items:
... print j.to_text()
...
"v=spf1 ip4:..."
NS:
>>> ns = dns.resolver.query("unixy.net", 'NS')
>>> for i in ns.response.answer:
... for j in i.items:
... print j.to_text()
...
ns2.unixy.net.
ns1.unixy.net.
You can get most records following the same pattern. Multiple responses queries are stored in a list. So looping is sometimes necessary (ex:multiple A and NS recs).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With