Thumbnail image

DNS Check

During an engagement we were working on some dns updates as part of a CI/CD integration and deployment.

A colleague at the time was having issue in validating if dns changes had propagated properly between internal and external dns servers. I put together a quick python script in order to assist with this.

import socket
import time
import os

# ANSI escape codes for text color
GREEN = "\033[92m"
RED = "\033[91m"
RESET = "\033[0m"

def perform_dns_lookup(domain, dns_servers=None):
    if dns_servers is None:
        dns_servers = ['9.9.9.9', '8.8.8.8', '1.1.1.1']  # Add more DNS servers as needed
    try:
        results = {}
        for server in dns_servers:
            addresses = set()
            try:
                for result in socket.getaddrinfo(domain, None, socket.AF_INET, socket.SOCK_STREAM, socket.SOL_TCP, socket.AI_CANONNAME):
                    ip_address = result[4][0]
                    addresses.add(ip_address)
            except socket.gaierror:
                addresses.add("Unable to resolve")

            results[server] = {"addresses": list(addresses)}
        return results
    except Exception as e:
        print(f"Error: {e}")
        return None

def main():
    domains = ["corebits.ca"]

    while True:
        os.system('cls' if os.name == 'nt' else 'clear')  # Clear the terminal screen

        domain_results = {domain: perform_dns_lookup(domain) for domain in domains}

        matching_addresses = all(
            result == domain_results[domains[0]] for result in domain_results.values()
        )

        if matching_addresses:
            print(f"{GREEN}The results are a match.{RESET}")
        else:
            print(f"{RED}The results are different.{RESET}")

        for domain, results in domain_results.items():
            print(f"Results for {domain}:")
            for server, data in results.items():
                addresses = ", ".join(data["addresses"])
                print(f"  Server: {server}")
                print(f"    Addresses: {addresses}")

        time.sleep(5)

if __name__ == "__main__":
    main()

While many tools exist for performing dns checks, not all environments allow the use of or access to resources. Sometimes a simple python script will allow you to get things done quickly within your environment.