1

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: domain1\010.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 \010 and then nothing happens. Can you help me with this? Thanks!

4
  • What's the encoding of the file domainfile.txt? I think this might be the problem. Commented Jan 27, 2020 at 14:03
  • If you want to check the encoding on a unix-system, run the command: file <filename> Commented Jan 27, 2020 at 14:03
  • 1
    I suspect the issue is more likely unstripped line endings. Commented Jan 27, 2020 at 14:09
  • Hey, Thanks for quick answer :) domainfile.txt is just text/plain; charset=us-ascii file. Do you recommend to change it to utf-8 or something else? Commented Jan 27, 2020 at 14:11

1 Answer 1

1

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') 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! That solved my problem. I abbadon readlines() at the beginning and don't know why :)
As of 2024 dns.resolver.query() is deprecated :<

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.