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

Question:

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.

Asked By: orokusaki

||

Answers:

Something like this should work to at least get the value for the URL, I used google.com for the example.

import pycurl
import StringIO
url = "whatsmyip.us/dns_txt.php?host=google.com"
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.HTTPHEADER, ["Accept:"])
txtcurl = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, txtcurl.write)
c.perform

data = txtcurl.getvalue()
data = data.replace("Done!", "")
print data

I did not test any of this but pulled it from a previous project.

Best of luck!

Answered By: sbrichards

This is easy using dnspython. Here is an example:

import dns.resolver
print dns.resolver.resolve("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] 
Answered By: 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.

Answered By: dharmatron

Found another way to get list of all TXT records for a domain using dnspython.

import dns.resolver
[dns_record.to_text() for dns_record in dns.resolver.resolve("your-domain-here", "TXT").rrset]
Answered By: lalit suthar

update 2022/11/20

# -*- coding:utf-8 -*-
# Copyright (c) DadouLab.SIG MIT

import dns
import dns.query
import dns.resolver
import logging

logger = logging.getLogger(__name__)


class Digger(object):
    def __init__(self, resolvers=["1.1.1.1"]):
        self.mResolver = dns.resolver.Resolver()
        self.mResolver.timeout = 1
        self.mResolver.lifetime = 0.5
        self.mResolver.nameservers = resolvers
        self.spec_query_type = ['CNAME', 'TXT', 'MX', 'NS', 'SRV', 'CAA']

    def query(self, domain, query_type="A"):
        """
        answer = dns.resolver.resolve("_dnsauth.test.com", "TXT").rrset
        for dns_record in answer:
            print(dns_record.to_text())
        """
        try:
            query_type = query_type.upper()
            answer = self.mResolver.resolve(domain, query_type, raise_on_no_answer=False)
            answer_raw = answer.chaining_result.answer.to_text()
            logger.info("resolved response data => {}".format(answer_raw))
            if query_type in self.spec_query_type:
                records = [data.to_text() for data in answer]
            else:
                records = [data.address for data in answer]
            return records
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer,
                dns.resolver.NoNameservers, dns.exception.Timeout) as error:
            logger.warning("resolved error => {}".format(error))
            return

    def is_valid(self, domain, query_type="A"):
        try:
            self.mResolver.resolve(domain, query_type, raise_on_no_answer=False)
            return True
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer,
                dns.resolver.NoNameservers, dns.exception.Timeout) as error:
            logger.warning("resolved error => {}".format(error))
        return


if __name__ == '__main__':
    dig = Digger()
    print(dig.query("www.example.com", query_type="A"))

Answered By: Ershan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.