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!"
The results are returned in the format ‘XX dns_entry’, so you can do:
Now you can compare against this list.