Recently I was involved in a project which required me to perform bulk lookups for more than 500 domains. The scope of the project was to validate if the domains have a name server published, if they have MX servers published and if they have SPF records published. As you probably understand, this is not a fun job if we are to do it manually for each domain. After the first five domains, the activity becomes boring unless we use some scripting to actually perform the bulk lookup using python.
Here below is the script I used to build a list with the SPF records for the domains in the file domains.txt.
Basically, we use the subprocess module so we can access the nslookup command under Windows then we process the output to find the string spf in the txt records published for the specific domain name. In case the domain doesn’t have any SPF record published, we just print “None”.
Here, we query the Google DNS, we could easily use the input function to query a dns server of our choice.
import subprocess
with open('domains.txt', 'r') as my_domain_src:
domain_list = my_domain_src.read().splitlines()
for my_domain in domain_list:
MyOut = subprocess.Popen(["nslookup", "-type=txt", my_domain, "8.8.8.8"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = MyOut.communicate()
my_stdout = str(stdout)
my_stdout2 = my_stdout.split('"')
if "spf" in my_stdout:
for x in my_stdout2:
if "spf" in x:
print(f'{my_domain},{x}')
else:
print(f'{my_domain},None')
Recent Comments