Created
August 4, 2022 12:47
-
-
Save wilpig/d7bcc2fd8a6f833fab2791e4e91e8ab6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
import argparse | |
import dns.resolver | |
import sys | |
import time | |
try: | |
import whois | |
except ImportError or ModuleNotFoundError: | |
print ('python-whois module is missing run pip3 install python-whois') | |
sys.exit(1) | |
# set up the command line options | |
parser = argparse.ArgumentParser(description='Python script to check infoblox cache response and clear invalid cache entries') | |
parser.add_argument('-d',"--domain", required=False, default='storefront.hap1017.onprem.varian.com', help='Domain to check infoblox response for', action="store") | |
parser.add_argument("--debug", required=False, default=False, help='Enable debug messages', action="store_true") | |
args = parser.parse_args() | |
debug=args.debug | |
url=args.domain | |
def getns(url): | |
# create a dns resolver and point it to use google dns | |
r = dns.resolver.Resolver() | |
r.nameservers = ['8.8.8.8'] | |
ns=[] | |
w=whois.whois(url) | |
for x in w.name_servers: | |
for y in r.resolve(x): | |
ns.append(y.to_text()) | |
# filter to keep unique responses | |
ns=list(set(ns)) | |
# if debug: | |
# print(ns) | |
return ns | |
def lookup(url): | |
error=False | |
res = dns.resolver.Resolver() | |
if debug: | |
print ('checking '+str(res.nameservers)+' for '+url) | |
try: | |
test=res.resolve(url,'A') | |
except dns.resolver.NXDOMAIN: | |
print('bad lookup') | |
error=True | |
except Exception as e: | |
print(e) | |
error=True | |
return error | |
def sourcelookup(url): | |
error=False | |
s = dns.resolver.Resolver() | |
if url == 'storefront.hap1017.onprem.varian.com': | |
s.nameservers=['10.100.182.132','10.100.182.133'] | |
else: | |
s.nameservers=getns(url) | |
if debug: | |
print ('checking '+str(s.nameservers)+' for '+url) | |
try: | |
test=s.resolve(url,'A') | |
except Exception as e: | |
print(e) | |
error=True | |
return error | |
while True: | |
lookupbad=False | |
while lookup(url): | |
print ('checking forwarder...') | |
while sourcelookup(url): | |
print ('sourcelookup failed waiting 5 seconds to try again') | |
time.sleep(5) | |
print ('infoblox failed response... sourcelookup good, clear cache here, check again in 60 seconds') | |
time.sleep(60) | |
print ('checking again in 60 seconds...') | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment