Skip to content

Instantly share code, notes, and snippets.

@adionditsak
Last active August 29, 2015 14:04
Show Gist options
  • Save adionditsak/44551dd477dff14d5a5b to your computer and use it in GitHub Desktop.
Save adionditsak/44551dd477dff14d5a5b to your computer and use it in GitHub Desktop.
Simple DNS + SMTP test script - writes to CSV
#!/usr/bin/env python3
import sys
import csv
import random
import time
import smtplib
import dns.resolver
class DNS():
""" MX and NS test to CSV """
def __init__(self):
""" init """
self.mx_test_csv = './results/dns_test%s.csv' % time.strftime("%H%M%S")
with open(self.mx_test_csv, 'w+') as f:
f.write('domain,response\n')
def check_domains(self):
""" loop through domains """
domains = csv.reader(open('./csv/host_domains.csv'))
for d in domains:
print('%s -' % d[0])
self.mx(d[0])
self.ns(d[0])
self.log('', '')
def log(self, domain, resp):
with open(self.mx_test_csv, "a") as f:
f.write('%s, %s\n' % (domain, resp))
def mx(self, domain):
""" domain MX testing """
self.domain = domain
try:
for mx in dns.resolver.query(self.domain, 'MX'):
self.log(self.domain, mx)
print(mx.to_text())
except:
self.log(self.domain, 'Error')
print('Error')
def ns(self, domain):
""" domain NS testing """
self.domain = domain
try:
for ns in dns.resolver.query(self.domain, 'NS'):
self.log(self.domain, ns)
print(ns.to_text())
except:
self.log(self.domain, 'Error')
print('Error')
class SMTP():
""" SMTP test though mailserver to CSV """
def __init__(self):
""" init """
self.smtp_test_csv = './results/ss_smtp_test%s.csv' % time.strftime("%H%M%S")
with open(self.smtp_test_csv, 'w+') as f:
f.write('domain,response_code\n')
def check_domains(self):
""" loop through domains """
domains = csv.reader(open('./csv/host_domains.csv'))
for d in domains:
print('%s -' % d[0])
self.ss_smtp('[email protected]', 'sstest@%s' % d[0])
def log(self, to_mail, code):
""" write success/error to csv """
with open(self.smtp_test_csv, "a") as f:
f.write('%s, "%s"\n' % (to_mail.replace('sstest@', ''), code))
def ss_smtp(self, from_mail, to_mail):
""" SMTP testing """
self.gw = ['mxgw01.domain.net', 'mxgw02.domain.net', 'mxgw03.domain.net']
self.server = smtplib.SMTP(random.choice(self.gw), 25)
#self.server.set_debuglevel(1) # smtp debug mode
self.m = 'Subject: SS test\n\nHello! SS test.\nSorry for the inconvenience.'
try:
self.server.sendmail(from_mail, to_mail, self.m)
self.log(to_mail, 'Success')
print('Sent.')
except (smtplib.SMTPRecipientsRefused, smtplib.SMTPSenderRefused) as err:
self.log(to_mail, 'Error - %s' % err)
print('Error.')
if __name__ == '__main__':
if len(sys.argv) > 1:
if sys.argv[1] == 'dns':
mx = DNS() # DNS() init
mx.check_domains()
elif sys.argv[1] == 'smtp':
smtp = SMTP() # SMTP() init
smtp.check_domains()
else:
print('')
print('Use dns (MX and NS test) or smtp (SMTP test) as an argument.')
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment