Python – Scan FQDNs and Tell you the Location of the Resolved IPs

Site A: 10.10.10.
Site B: 20.20.20.

You want to change 10.10.10. and 20.20.20. to the subnets you use.

#This script will scan FQDNS from a text file called "dr-dns.txt", resolve the FQDNs, and state the location based on the IP Resolved.

import os
import socket

def resolve_fqdn(fqdn):
        ip_address = socket.gethostbyname(fqdn)
        return ip_address
    except socket.gaierror:
        return None

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

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

with open(filename, "r") as file:
    for line in file:
        fqdn = line.strip()
        ip = resolve_fqdn(fqdn)
        if ip is not None:
            category = categorize_ip(ip)
            print(f"{fqdn} - {ip} - {category}")
            print(f"{fqdn} - Unable to resolve IP address")