Python – Find if a DNS Record Exists both Internally and Externally

# Author: Kerry Cordero
# Version: 1.0.0
# Description: This script will scan both Internal and External DNS

import dns.resolver
from prettytable import PrettyTable
from colorama import Fore, init

# Initialize colorama
init(autoreset=True)

def check_dns_record(fqdn, server):
    resolver = dns.resolver.Resolver()
    resolver.nameservers = [server]
    resolver.timeout = 10
    resolver.lifetime = 10

    try:
        answers = resolver.resolve(fqdn, 'A')
        for rdata in answers:
            return str(rdata)
    except dns.resolver.NXDOMAIN:
        return None

def main():
    fqdn = input("Please enter the FQDN: ")

    internal_dns = "8.8.8.8"
    external_dns = "4.2.2.2"

    internal_response = check_dns_record(fqdn, internal_dns)
    external_response = check_dns_record(fqdn, external_dns)

    table = PrettyTable()
    table.field_names = ["DNS Server", "Response"]

    if internal_response:
        table.add_row([internal_dns, f"{Fore.GREEN}Yes{Fore.RESET}, IP: {internal_response}"])
    else:
        table.add_row([internal_dns, f"{Fore.RED}No{Fore.RESET}"])

    if external_response:
        table.add_row([external_dns, f"{Fore.GREEN}Yes{Fore.RESET}, IP: {external_response}"])
    else:
        table.add_row([external_dns, f"{Fore.RED}No{Fore.RESET}"])

    print(table)

if __name__ == "__main__":
    main()