I am writing a code which will resolve CNAME from the list of domains but my current script only resolve the first line of the list.txt and skips the rest.
I check "dl" import all the lines into list by running print(dl) but dns.resolver is not running check on each list data.
My sample Code is:
#/usr/bin/python3
import dns.resolver
d = open("list.txt" , "r")
dl = d.read().splitlines()
try:
for dom in dl:
answer = dns.resolver.resolve(dom, 'CNAME')
for rdata in answer:
print("CNAME", ':', rdata.to_text())
except Exception as e:
print(e)
list.txt is:
www.google.com
www.github.com
www.facebook.com
Desired output is:
CNAME : www.gcaname.com
CNAME : www.gcaname.com
The DNS response does not contain an answer to the question: www.github.com. IN CNAME
CodePudding user response:
try this:
answers = dns.resolver.query('mail.google.com', 'CNAME')
print ' query qname:', answers.qname, ' num ans.', len(answers)
for rdata in answers:
print ' cname target address:', rdata.target
CodePudding user response:
This worked:
for dom in dl:
try:
answer = dns.resolver.resolve(dom, 'CNAME')
for rdata in answer:
print("CNAME of %s" % dom, ':', rdata.to_text())
except Exception as e:
pass
