Hsin
Hsin

Reputation: 17

Python Nslookup domain list issue

New to python and I am using Python 3.7

I have a list of domains. I wanna read these domains from a text file and then check nslookup. But this code is not working. What's wrong with this code? Please let me know the solution.

import dns.resolver,os
new_txt = open("Domain_check.txt","w")
with open(os.getcwd()+"\\\Domains.txt", "r") as f:
 for date in f:
  dateb = str(date)
  dateb = dateb.replace("\n"," ")
  answers = dns.resolver.resolve(dateb, 'A')
  for rdate in answers:
   b = str(dateb) + str(rdate)
   new_txt.write(b)

My Domain list is : google.com、yam.com

Try to result as below: enter image description here

Upvotes: 0

Views: 252

Answers (1)

Jan Wilamowski
Jan Wilamowski

Reputation: 3608

The error message The DNS query name does not exist: yahoo.com\032. gives a hint: there's an additional character at the end of the address. This is because when you read the Domains.txt file, you replace the linebreaks with a space - incidentally, that's code 32 in ASCII. Do this instead:

import dns.resolver

with open("Domains.txt", "r") as in_file, \
     open("Domain_check.txt", "w") as out_file:

    for line in in_file:
        qname = line.strip()
        answers = dns.resolver.resolve(qname, 'A')
        for rdata in answers:
            output = qname + ' ' + str(rdata)
            out_file.write(output)

I fixed a few other issues with the code. Please feel free to ask if anything is unclear.

Upvotes: 1

Related Questions