Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python dns.resolver get DNS record type

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

like image 316
Jim Avatar asked Dec 21 '22 14:12

Jim


2 Answers

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
like image 131
Mike Barkas Avatar answered Dec 27 '22 05:12

Mike Barkas


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).

like image 37
Joe H Avatar answered Dec 27 '22 04:12

Joe H