Question

I consider myself a very beginner at python(and programming in general!), but I am working though "learn python the hard way" by Zed A Shaw and slowing picking things up. I'm writing a little script to check if the live mx records of a domain are to be as expected and have not been changed (long story) and so far I have the following:

import dns.resolver
domain = 'bbc.co.uk'
for x in dns.resolver.query(domain,'MX',):
    print x.to_text()

This uses the dnspython module to spit out the mailhost and the preference number. What I need to do now is compare this output to the two expected results, so for bbc.co.uk those would be cluster1a.eu.messagelabs.com. & cluster1.eu.messagelabs.com. (Their ordering changes depending on the current preference number)

I thought the best way to do this would to be to add the expected results to a array/list and have the script try and compare the output to the array/list and provide a true or false statement, but after spending all day trying different arrangements of code this is proving to be beyond my understanding so far.

Eventually I would like it to alert myself or my colleagues if the result come up false, but that can wait until later as I haven't decided on the best method for this to be implemented. Would any kind soul be able to give me a rough outline of what the best practice would be to achieve the result I am hoping for?

I appreciate anyone taking the time to read this :)

Thank you, Chris

EDIT:This appears to do exactly what I was hoping for, thank you everyone for you help!

import dns.resolver
domain = 'bbc.co.uk'
expected_responses = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
for x in dns.resolver.query(domain, 'MX'):
        if x.to_text().split()[1] not in expected_responses:
                print "Unexpected MX record found!"
        else:
                print x.to_text().split()[1] + " OK!"
Was it helpful?

Solution

The results are returned in the format 'XX dns_entry', so you can do:

import dns.resolver
domain = 'bbc.co.uk'
results = []
for x in dns.resolver.query(domain,'MX',):
    results.append(x.to_text().split(' ')[1])
print results

>>> ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']

Now you can compare against this list.

OTHER TIPS

You mean:

x.to_text() in {'cluster1a.eu.messagelabs.com', 'cluster1.eu.messagelabs.com'}

?

Alright, so first you have to drop the leading number from what x.to_text() returns:

txt = '20 cluster1a.eu.messagelabs.com.' # an example x.to_text()
txt = txt.split()[1] # Get rid of everything before (and including) the first space.

You can do that loopily, or with a list comprehension:

records = [x.to_text().split()[1] for x in dns.resolver.query(domain, 'MX')]

Then just make sure everything you expect is in the records

expected = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
if False in [val in records for val in expected] or len(records) != len(expected):
    # Die.

What about?

import dns.resolver

expected_domains = set(['cluster1a.eu.messagelabs.com.', 'cluster1.eu.messagelabs.com.'])
domains = set(str(mx.exchange) for mx in dns.resolver.query('bbc.co.uk', 'MX'))
if not domains.issuperset(expected_domains):
    print("Missing MX domains:", ", ".join(expected_domains - domains))

EDIT:This appears to do exactly what I was hoping for, thank you everyone for you help!

import dns.resolver
domain = 'bbc.co.uk'
expected_responses = ['cluster1.eu.messagelabs.com.', 'cluster1a.eu.messagelabs.com.']
for x in dns.resolver.query(domain, 'MX'):
        if x.to_text().split()[1] not in expected_responses:
                print "Unexpected MX record found!"
        else:
                print x.to_text().split()[1] + " OK!"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top