Skip to content

Instantly share code, notes, and snippets.

@lesiki
Created March 30, 2022 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesiki/7e151c639f6cbd9b13223781421cb60c to your computer and use it in GitHub Desktop.
Save lesiki/7e151c639f6cbd9b13223781421cb60c to your computer and use it in GitHub Desktop.
Script to identify the mail host for a set of domains
import dns.resolver
input_domains = [
"1.com",
"2.com",
"3.com",
"etc.com",
]
for domain in input_domains:
answer = None
try:
answers = dns.resolver.resolve(domain, 'MX')
answers = sorted(answers, key = lambda a: a.preference)
answer = answers[0] if len(answers) > 0 else None
except Exception as e:
answer = None
if answer is None:
print(f"{domain},,")
else:
best_guess = ""
exchange = str(answer.exchange)
if "google" in exchange:
best_guess = 'google'
elif "outlook.com" in exchange:
best_guess = "outlook"
elif "zoho" in exchange:
best_guess = "zoho"
print(f"{domain}, {exchange}, {best_guess}")
@lesiki
Copy link
Author

lesiki commented Mar 30, 2022

Requires dnspython:

  • Create a virtualenv
  • Install dnspython
  • run python mx_lookup.py | tee output.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment