MX Record lookup and check

Christopher Long picture Christopher Long · Dec 2, 2010 · Viewed 21.8k times · Source

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

Answer

Paulo Scardine picture Paulo Scardine · Dec 2, 2010

With [dnspython][1] module (not built-in, you must pip install it):

>>> import dns.resolver
>>> domain = 'hotmail.com'
>>> for x in dns.resolver.resolve(domain, 'MX'):
...     print(x.to_text())
...
5 mx3.hotmail.com.
5 mx4.hotmail.com.
5 mx1.hotmail.com.
5 mx2.hotmail.com.