问题
I need to create a tool that will check a domains live mx records against what should be expected (we have had issues with some of our staff fiddling with them and causing all incoming mail to redirected into the void)
Now I won't lie, I'm not a competent programmer in the slightest! I'm about 40 pages into "dive into python" and can read and understand the most basic code. But I'm willing to learn rather than just being given an answer.
So would anyone be able to suggest which language I should be using?
I was thinking of using python and starting with something along the lines of using 0s.system() to do a (dig +nocmd domain.com mx +noall +answer) to pull up the records, I then get a bit confused about how to compare this to a existing set of records.
Sorry if that all sounds like nonsense!
Thanks Chris
回答1:
With dnspython module (not built-in, you must pip install
it):
>>> import dns.resolver
>>> domain = 'hotmail.com'
>>> for x in dns.resolver.query(domain, 'MX'):
... print(x.to_text())
...
5 mx3.hotmail.com.
5 mx4.hotmail.com.
5 mx1.hotmail.com.
5 mx2.hotmail.com.
[update]
For python 3 it is pip install dnspython3
.
回答2:
Take a look at dnspython, a module that should do the lookups for you just fine without needing to resort to system calls.
回答3:
Why not use nslookup? This code should be compatible with 2.6+
import os
import re
__query = 'nslookup -q=mx {0}'
__pattern = '\*\*\sserver\scan\'t\sfind'
def check_for_mx_record(domain):
try:
command = __query.format(domain)
with os.popen(command) as response:
result = response.readlines()
return all(re.match(__pattern,l) == None for l in result)
except Exception:
return False
来源:https://stackoverflow.com/questions/4336849/mx-record-lookup-and-check