Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check the value of a DNS TXT record for a host?

Tags:

python

dns

I'm looking to verify domain ownership via a script, specifically a Python script, and would like know how to lookup the value of a DNS TXT entry. I know there are services and websites out there for this, but I would like to do it with a script.

like image 226
orokusaki Avatar asked Jul 29 '12 01:07

orokusaki


2 Answers

This is easy using dnspython. Here is an example:

import dns.resolver
print dns.resolver.query("aaa.asdflkjsadf.notatallsuspicio.us","TXT").response.answer[0][-1].strings[0]

This gives the following output:

PnCcKpPiGlLfApDbDoEcBbPjIfBnLpFaAaObAaAaMhNgNbIfPbHkMiEfPpGgJfOcPnLdDjBeHkOjFjIbPbIoKhIjHfJlAhAhFgGbGgNlMgKmFkLgNfBjMbCoBeNbGeOnAeHgLmKoFlLhLmDcKlEdEbDpFeHkFaBlGnHiOnChIoMlIhBgOnFfKoEhDnFkKfDaMgHbJhMgPgMjGiAoJpKjKkPaIcAdGiMbIbBbAfEiKjNbCeFoElKgOePmGjJaImL

Another option is to use dig in subprocess:

import subprocess

print subprocess.Popen(["dig","-t","txt","aaa.asdflkjsadf.notatallsuspicio.us","+short"], stdout=subprocess.PIPE).communicate()[0] 
like image 133
jordanm Avatar answered Nov 10 '22 00:11

jordanm


This may be overly simplified, but if all you want is a quick read of the TXT record and don't mind dealing with parsing the result separately:

nslookup -q=txt somedomain.com

I found this did what I needed, short & sweet.

like image 34
dharmatron Avatar answered Nov 09 '22 23:11

dharmatron