Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we get TXT, CNAME and SOA records from dnspython?

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?

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?

like image 958
Shabareesh Avatar asked Dec 12 '12 14:12

Shabareesh


People also ask

How do I find my SOA records?

First, you need to get the current serial numberOpen a command window. Type nslookup and press [Enter]. Switch to querying SOA records by typing set type=soa and press [Enter]. Type the name of the domain name in question and press [Enter].

How do I find MX records in Python?

Finding MX RecordMX Record: ASPMX.L.GOOGLE.COM. MX Record: ALT1.ASPMX.L.GOOGLE.COM. MX Record: ALT2.ASPMX.L.GOOGLE.COM.

What type of record is used to store canonical name information in DNS?

IP Version 6 Address record (AAAA Record)—stores a hostname and its corresponding IPv6 address. Canonical Name record (CNAME Record)—can be used to alias a hostname to another hostname.


1 Answers

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

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.

To figure out the appropriate attributes, change your for loop temporarily to the following:

   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.

Lastly, you could look at the source if the library is in python or if not, then if the C code is available.

(And to answer your question:)

Here are examples of TXT, CNAME and SOA queries:

TXT

http://www.dnspython.org/docs/1.15.0/dns.rdtypes.txtbase.TXTBase-class.html#section-InstanceVariables

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

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

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)
like image 146
Lars Nordin Avatar answered Sep 19 '22 10:09

Lars Nordin