279 lines
8.2 KiB
Python
Executable File
279 lines
8.2 KiB
Python
Executable File
import subprocess
|
|
import re # YAY REVERSE ENGINEER... wait its just regex
|
|
import readline
|
|
import os
|
|
#Made this work with linux magic, test
|
|
|
|
#Define Colours
|
|
class bcolors:
|
|
RED = "\033[1;31m"
|
|
BLUE = "\033[1;34m"
|
|
CYAN = "\033[1;36m"
|
|
GREEN = "\033[0;32m"
|
|
RESET = "\033[0;0m"
|
|
BOLD = "\033[;1m"
|
|
REVERSE = "\033[;7m"
|
|
|
|
#FIXME: Have any CNAMES returned do a lookup and print it after the record.
|
|
|
|
inInput = ""
|
|
partDomain = ""
|
|
running = 1
|
|
pingOn = "p"
|
|
whoOn = "W"
|
|
phrases = ["ping", "whois", "quit", "clear", "help", "nameserver"]
|
|
|
|
|
|
def openFile(filename):
|
|
try:
|
|
myfile = open(filename)
|
|
except Exception as E:
|
|
print E
|
|
print 'Creating default config file'
|
|
myfile = open(filename, 'w')
|
|
myfile.write('records=A:MX:TXT:NS:SRV:SOA\n')
|
|
myfile.write('subdomains=www:mail')
|
|
myfile.close()
|
|
quit()
|
|
return myfile
|
|
|
|
def closeFile(fileObj):
|
|
try:
|
|
fileObj.close()
|
|
except Exception as E:
|
|
print E
|
|
return 1
|
|
return 0
|
|
#Check and see if python can do pairs
|
|
def splitLines(confline):
|
|
tmpLine = confline.split('\n')
|
|
retval = []
|
|
for line in tmpLine:
|
|
if line != '':
|
|
tmpLines = line.split('=')
|
|
retval.append(tmpLines[1].split(':'))
|
|
return retval
|
|
|
|
def printHelp():
|
|
print bcolors.BLUE
|
|
print "========================================================"
|
|
print "= help - This screen ="
|
|
print "= clear - Clears the screen ="
|
|
print "= ping - Enables/Disables pinging the IP Address ="
|
|
print "= whois - Enables/Disables whois lookups ="
|
|
print "========================================================"
|
|
print bcolors.RESET
|
|
|
|
def compareSOA(inNameservers, inDomain, origSOA):
|
|
dnsServer = '8.8.8.8'
|
|
soaRecords = []
|
|
count = 0
|
|
outSOA = ''
|
|
returnmsg = "SOA Good"
|
|
for nameserver in inNameservers:
|
|
if nameserver != '':
|
|
dnsServer = "@" + nameserver
|
|
try:
|
|
outSOA = subprocess.check_output(["dig", "SOA", dnsServer, "+short", inDomain])
|
|
except subprocess.CalledProcessError as e:
|
|
output = e.output
|
|
returncode = e.returncode
|
|
|
|
|
|
outSOAa = outSOA.split(' ')
|
|
#print outSOAa[2]
|
|
if len(outSOAa) != 1:
|
|
soaRecords.append(outSOAa[2])
|
|
for soa in soaRecords:
|
|
if soa != soaRecords[0]:
|
|
return "SOA Records don't Match"
|
|
return "SOA good"
|
|
|
|
#print soaRecords
|
|
|
|
def checkIfIP(in1):
|
|
count = 0
|
|
if len(in1) != 4:
|
|
return 0
|
|
for dom in in1:
|
|
if dom.isdigit():
|
|
count += 1
|
|
else:
|
|
return 0
|
|
if count == 4:
|
|
return 1
|
|
|
|
|
|
def checkPhrase(inputs):
|
|
for word in phrases:
|
|
if inputs == word:
|
|
options(inputs)
|
|
return 1
|
|
return 0
|
|
|
|
def options(inputs):
|
|
global whoOn
|
|
global pingOn
|
|
|
|
if inputs == "ping":
|
|
if pingOn == "P":
|
|
pingOn = "p"
|
|
else:
|
|
pingOn = "P"
|
|
if inputs == "whois":
|
|
if whoOn == "W":
|
|
whoOn = "w"
|
|
else:
|
|
whoOn = "W"
|
|
if inputs == "clear":
|
|
subprocess.call("clear")
|
|
if inputs == "help":
|
|
printHelp()
|
|
|
|
def iterate(inArray, pre):
|
|
count = 0
|
|
while count != len(inArray) - 1:
|
|
#print checkIfIP(inArray[count].split('.'))
|
|
if pre == "TXT" or pre == "SOA":
|
|
print bcolors.CYAN + pre, bcolors.GREEN + inArray[count] + bcolors.RESET
|
|
else:
|
|
if checkIfIP(inArray[count].split('.')) == 0:
|
|
tmpVal = isCNAME(inArray[count])
|
|
|
|
print bcolors.CYAN + pre, bcolors.GREEN + inArray[count] + bcolors.RESET + " -> " + bcolors.GREEN + tmpVal[0] + bcolors.RESET
|
|
else:
|
|
print bcolors.CYAN + pre, bcolors.GREEN + inArray[count] + bcolors.RESET
|
|
count = count + 1
|
|
|
|
|
|
def iterateMX(inArray, pre):
|
|
count = 0
|
|
while count != len(inArray) - 1:
|
|
tmp = inArray[count].split(' ')
|
|
tmpVal = isCNAME(tmp[1])
|
|
print bcolors.CYAN + pre, bcolors.GREEN + inArray[count] + bcolors.RESET + " -> " + bcolors.GREEN + tmpVal[0] + bcolors.RESET
|
|
count = count + 1
|
|
|
|
def parseWhois(whioisInfo):
|
|
NS = re.compile("^[Nn]")
|
|
D = re.compile("^[Dd]")
|
|
R = re.compile("^[Rr]")
|
|
U = re.compile("^[Uu]")
|
|
S = re.compile("Status")
|
|
L = re.compile("^[Ll]")
|
|
E = re.compile("^[Ee]")
|
|
|
|
for str in whioisInfo:
|
|
str = str.strip()
|
|
if NS.match(str) or D.match(str) or R.match(str) or U.match(str) or S.match(str) or L.match(str) or E.match(str):
|
|
if "domain" in str or "Domain" in str and "Name" not in str or "Status" in str:
|
|
if "name" not in str and "ID" not in str:
|
|
if "ok" in str or "OK" in str:
|
|
print bcolors.GREEN + str + bcolors.RESET
|
|
else:
|
|
print bcolors.RED + str + bcolors.RESET
|
|
else:
|
|
if "Expired" in str or "expired" in str:
|
|
print bcolors.RED + str + bcolors.RESET
|
|
else:
|
|
print str
|
|
|
|
def whois(inDomain):
|
|
lookup = subprocess.check_output(["whois", inDomain])
|
|
print "========================================================"
|
|
test = lookup.split("\n")
|
|
#iterate(test, "")
|
|
parseWhois(test)
|
|
|
|
def dig(inDomain, record):
|
|
try:
|
|
dig = subprocess.check_output(["dig", record, "+short", inDomain])
|
|
except Exception as e:
|
|
dig = "error"
|
|
print str(e)
|
|
print "========================================================"
|
|
test = dig.split("\n")
|
|
iterate(test, record)
|
|
return test
|
|
|
|
def newdig(inDomain, record, sub=""):
|
|
try:
|
|
dig = subprocess.check_output(["dig", record, "+short", inDomain])
|
|
except Exception as e:
|
|
dig = "error"
|
|
print str(e)
|
|
#print "========================================================"
|
|
test = dig.split("\n")
|
|
#iterate(test, record)
|
|
return test, record, sub
|
|
|
|
def ipLookup(inDomain):
|
|
host = "N/A"
|
|
ipLook = subprocess.check_output(["geoiplookup", inDomain])
|
|
try:
|
|
host = subprocess.check_output(["host", inDomain])
|
|
returncode = 0
|
|
except subprocess.CalledProcessError as e:
|
|
output = e.output
|
|
returncode = e.returncode
|
|
|
|
print '\n', ipLook, '\n', "PTR: ", host
|
|
|
|
def ipPing(inDomain):
|
|
ping = subprocess.call(["ping", "-c 2", inDomain])
|
|
#iterate(ping, "")
|
|
|
|
def getInput():
|
|
global inInput
|
|
global partDomain
|
|
global running
|
|
inInput = raw_input("["+ pingOn +"][" + whoOn + "] Please enter a domain/IP: ")
|
|
inInput = inInput.strip()
|
|
inInput = inInput.lower()
|
|
partDomain = inInput.split(".")
|
|
if inInput == "quit":
|
|
running = 0
|
|
|
|
def isCNAME(CNAME):
|
|
retval = newdig(CNAME, "A")
|
|
return retval[0]
|
|
|
|
|
|
#Main Loop
|
|
testFile = openFile(os.path.expanduser('~/.httpy'))
|
|
test = testFile.read()
|
|
closeFile(testFile)
|
|
rec2check, sub2check = splitLines(test)
|
|
|
|
while running == 1:
|
|
getInput()
|
|
if checkPhrase(inInput) == 1 or len(partDomain) == 1:
|
|
pass
|
|
else:
|
|
if running == 0:
|
|
pass
|
|
else:
|
|
if checkIfIP(partDomain):
|
|
print "========================================================"
|
|
ipLookup(inInput)
|
|
if pingOn == "P":
|
|
print "========================================================"
|
|
ipPing(inInput)
|
|
|
|
else:
|
|
if whoOn == "W":
|
|
whois(inInput)
|
|
for sub in sub2check:
|
|
if sub != "":
|
|
tmpDom = sub + "." + inInput
|
|
subRec, subType, subDomain = newdig(tmpDom, "A", sub)
|
|
iterate(subRec, subDomain)
|
|
for rec in rec2check:
|
|
if rec != "":
|
|
if rec == "MX":
|
|
dnsRec, dnsType, blank = newdig(inInput, rec)
|
|
iterateMX(dnsRec, dnsType)
|
|
else:
|
|
dnsRec, dnsType, blank = newdig(inInput, rec)
|
|
iterate(dnsRec, dnsType)
|
|
print '\n' |