Skip to content
Advertisement

DNS Resolver does not contain answer

I’m trying to automate some of the tasks with python. I have to chcek if some domains are still in ours DNS servers. So searching through stack i found script with dns.resolver and tryied to suit it to my needs. So, the script looks like this:

import socket
import dns.resolver


mylist=[]
with open('domainfile.txt') as file:
    for line in file:
        mylist.append(line)
length = len(mylist)


resolver = dns.resolver.Resolver()
resolver.nameservers=[socket.gethostbyname('dns.example.com')]

for i in range(length):
    for rdata in resolver.query(mylist[i], 'CNAME') :
        print(rdata.target)

My domainfile.txt looks like this:

domain1
domain2
domain3
[...]

And error message i recived is:

Traceback (most recent call last): File “dnspython.py”, line 20, in for rdata in resolver.query(lista[i], ‘CNAME’) : File “/usr/local/lib/python3.7/dist-packages/dns/resolver.py”, line 1053, in query raise_on_no_answer) File “/usr/local/lib/python3.7/dist-packages/dns/resolver.py”, line 236, in init raise NoAnswer(response=response) dns.resolver.NoAnswer: The DNS response does not contain an answer to the question: domain110.example.com. IN CNAME

If I just insert my domain inside if statement instead of iterate through the mylist I get what I need. But when I try to do it through the list it adds to my domain 10 and then nothing happens. Can you help me with this? Thanks!

Advertisement

Answer

That seems expected according to the docs: http://www.dnspython.org/docs/1.14.0/dns.resolver.Resolver-class.html#query

Raises:

  • NoAnswer – the response did not contain an answer and raise_on_no_answer is True.

The reason it’s happening is probably because you haven’t removed line endings from your domains.


Here’s how I’d do it.

import socket
import dns.resolver


with open('domainlist.txt') as f:
    my_list = [line.strip() for line in f.readlines()]

resolver = dns.resolver.Resolver()
resolver.nameservers=[socket.gethostbyname('dns.example.com')]

for domain in my_list:
    try:
        q = resolver.query(domain, 'CNAME')
        for rdata in q:
            print(f'{domain}: {rdata.target}')
    except dns.resolver.NoAnswer:
        print(f'{domain}: No answer')

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement