Python – FQDN Scanner

# Author: Kerry Cordero
# Version: 1.0.0
# Description: This script will scan an FQDN file, and based on the IP Address, it will tag it with either STP or DR. It will use all the DNS servers listed under dns_servers. The output of the scanned results will be put into a table where the STP will be colored green, and the DR will be colored red.

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

def resolve_fqdn(fqdn, dns_servers):
    dns_responses = {}
    for dns_server in dns_servers:
        resolver = dns.resolver.Resolver()
        resolver.nameservers = [dns_server]
        try:
            answers = resolver.resolve(fqdn)
            for rdata in answers:
                dns_responses[dns_server] = rdata.to_text()
        except dns.resolver.NXDOMAIN:
            dns_responses[dns_server] = "Unable to resolve IP address"
        except Exception as e:
            dns_responses[dns_server] = str(e)
    return dns_responses

def categorize_ip(ip):
    if ip.startswith("10.10.10."):
        return "DR"
    elif ip.startswith("20.20.20."):
        return "STP"
    else:
        return "Unknown"

dns_servers = ["4.2.2.2", "1.1.1.1", "8.8.8.8", "9.9.9.9", "107.162.176.221", "107.162.234.197"]

filename = os.path.join(os.path.dirname(__file__), "dr-dns.txt")

table = PrettyTable()
table.field_names = ["FQDN"] + dns_servers
table.align = "l"

with open(filename, "r") as file:
    for line in file:
        fqdn = line.strip()
        dns_responses = resolve_fqdn(fqdn, dns_servers)
        row = [fqdn]
        for dns_server in dns_servers:
            ip = dns_responses.get(dns_server, "Unable to resolve IP address")
            if ip != "Unable to resolve IP address":
                category = categorize_ip(ip)
                if category == "DR":
                    color = Fore.RED
                elif category == "STP":
                    color = Fore.GREEN
                else:
                    color = Style.RESET_ALL
                row.append(f"{ip} ({color}{category}{Style.RESET_ALL})")
            else:
                row.append(ip)
        table.add_row(row)

print(table)

If you want to add multiple DR and Local subnets to match:

def categorize_ip(ip):
    if ip.startswith("10.10.10.") or ip.startswith("10.20.20."):
        return "DR"
    elif ip.startswith("20.20.20.") or ip.startswith("20.30.30."):
        return "STP"
    else:
        return "Unknown"