Board index Linux Python Scripts

Moderator: chandranjoy

dnsresolver.py

Postby chandranjoy » Sun Dec 25, 2016 8:55 am

Code: Select all
#!/usr/bin/env python
#Script by Jayachandran Palanisamy - Cygate Cloud - India team
import dns.resolver
import sys
import dns.query
import os.path
from optparse import OptionParser
import socket
import os
import re
import commands
from subprocess import call

class bcolors:
   HEADER = '\033[95m'
   OKBLUE = '\033[94m'
   OKGREEN = '\033[92m'
   WARNING = '\033[93m'
   FAIL = '\033[91m'
   ENDC = '\033[0m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'

def usage():
   print bcolors.FAIL + 'Please enter valid domain name.'
   print bcolors.WARNING + bcolors.BOLD + 'Usage: python dnsresolver.py domain.tld'
   sys.exit(0)

def main():
try:
   version = 'v1.0.1'
   script = 'DNSresolver'
   team = '|| Cygate Cloud - India Linux Team'
   __author__ = 'Jayachandran Palanisamy'
   __date__ = '2016-12-20 18:18:00'
   #Print Script version
   print bcolors.HEADER + script, version, team
   domain_name = sys.argv[1]

   if len(domain_name) > 253:
      usage()

   elif domain_name == '':
      usage()

   elif len(domain_name) < 5:
      usage()

   else:
      try:
         print ''
         print bcolors.HEADER + 'DNS records of the domain:', domain_name
         print '========================================'
         myresolver_NS = dns.resolver.query(domain_name, 'NS')
         print bcolors.OKBLUE + 'NS Record(s) of the domain:'
         for rdata_NS in myresolver_NS:
            print bcolors.WARNING, rdata_NS
         print bcolors.OKBLUE + '-------------------'
      except:
         print bcolors.FAIL, 'No NS record(s) found.'

      try:
         myresolver_A = dns.resolver.query(domain_name, 'A')
         print bcolors.OKBLUE + 'A Record(s) of the domain:'
         for rdata_A in myresolver_A:
            print bcolors.WARNING, rdata_A
         print bcolors.OKBLUE + '-------------------'
      except:
         print bcolors.FAIL, 'No A record(s) found.'

      try:
         myresolver_CNAME = dns.resolver.query(domain_name, 'CNAME')
         print bcolors.OKBLUE + 'WWW CNAME Record(s) of the domain found:'
         for rdata_CNAME in myresolver_CNAME:
            print bcolors.WARNING, rdata_CNAME
         print bcolors.OKBLUE + '-------------------'
      except:
         print bcolors.FAIL, 'WWW CNAME Record(s) of the domain not found:'

      try:
         myresolver_MX = dns.resolver.query(domain_name, 'MX')
         print bcolors.OKBLUE + 'MX Record(s) of the domain:'
         for rdata_MX in myresolver_MX:
            print bcolors.WARNING, rdata_MX.preference, rdata_MX.exchange
         print bcolors.OKBLUE + '-------------------'
      except:
         print bcolors.FAIL, 'No MX record(s) found.'

      try:
         myresolver_SPF = dns.resolver.query(domain_name, 'TXT')
         print bcolors.OKBLUE + 'SPF Record(s) of the domain:'
         for rdata_SPF in myresolver_SPF:
            print bcolors.WARNING, rdata_SPF
         print bcolors.OKBLUE + '-------------------'
      except:
         print bcolors.FAIL, 'No SPF record found.'

      try:
         #Get PTR info
         myresolver_PTR = socket.gethostbyaddr(domain_name)
         #name, alias, addresslist = socket.gethostbyaddr(domain_name)
         print bcolors.OKBLUE + 'PTR Record(s) of the domain:'
         print bcolors.WARNING, myresolver_PTR[0], myresolver_PTR[2]
         print bcolors.OKBLUE + '-------------------'
      except:
         print bcolors.FAIL, 'No PTR record found.'

      try:
         myresolver_SOA = dns.resolver.query(domain_name, 'SOA')
         print bcolors.OKBLUE + 'SOA Record(s) of the domain:'
         for rdata in myresolver_SOA:
            print bcolors.WARNING, 'Serial: %s' % (rdata.serial), '(YYYY-MM-DD nn format)'
            print bcolors.WARNING, 'Tech Email: %s' % (rdata.rname), '(Hostmaster Email Address)'
            print bcolors.WARNING, 'Refresh: %s' % (rdata.refresh), '(SOA Refresh Interval)'
            print bcolors.WARNING, 'Retry: %s' % (rdata.retry), '(SOA Retry Value)'
            print bcolors.WARNING, 'Expire: %s' % (rdata.expire), '(SOA Expire value)'
            print bcolors.WARNING, 'Minimum: %s' % (rdata.minimum), '(SOA Minimum TTL)'
            print bcolors.WARNING, 'P.DNS: %s' % (rdata.mname), '(Parent DNS server)'
         print bcolors.OKBLUE + '-------------------'
      except Exception, e:
         print bcolors.FAIL, e, 'No SOA record(s) found.'

except:
   usage()

if __name__ == "__main__":
    main()
chandranjoy
Site Admin
 
Posts: 283
Joined: Fri Oct 23, 2009 11:19 pm

Return to Python Scripts

Who is online

Users browsing this forum: No registered users and 1 guest


cron