-
-
Notifications
You must be signed in to change notification settings - Fork 640
/
dns_mx.py
52 lines (48 loc) · 2.12 KB
/
dns_mx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter04/dns_mx.py
# Looking up a mail domain - the part of an email address after the `@`
import argparse, dns.resolver
def resolve_hostname(hostname, indent=''):
"Print an A or AAAA record for `hostname`; follow CNAMEs if necessary."
indent = indent + ' '
answer = dns.resolver.query(hostname, 'A')
if answer.rrset is not None:
for record in answer:
print(indent, hostname, 'has A address', record.address)
return
answer = dns.resolver.query(hostname, 'AAAA')
if answer.rrset is not None:
for record in answer:
print(indent, hostname, 'has AAAA address', record.address)
return
answer = dns.resolver.query(hostname, 'CNAME')
if answer.rrset is not None:
record = answer[0]
cname = record.address
print(indent, hostname, 'is a CNAME alias for', cname) #?
resolve_hostname(cname, indent)
return
print(indent, 'ERROR: no A, AAAA, or CNAME records for', hostname)
def resolve_email_domain(domain):
"For an email address `name@domain` find its mail server IP addresses."
try:
answer = dns.resolver.query(domain, 'MX', raise_on_no_answer=False)
except dns.resolver.NXDOMAIN:
print('Error: No such domain', domain)
return
if answer.rrset is not None:
records = sorted(answer, key=lambda record: record.preference)
print('This domain has', len(records), 'MX records')
for record in records:
name = record.exchange.to_text(omit_final_dot=True)
print('Priority', record.preference)
resolve_hostname(name)
else:
print('This domain has no explicit MX records')
print('Attempting to resolve it as an A, AAAA, or CNAME')
resolve_hostname(domain)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Find mailserver IP address')
parser.add_argument('domain', help='domain that you want to send mail to')
resolve_email_domain(parser.parse_args().domain)