Last active
October 13, 2023 16:33
-
-
Save simonLeary42/71b6847fae7fedbed536ad58a0862da8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import sys | |
import socket | |
import subprocess | |
LOOKUPS = [ | |
"unity.rc.umass.edu.", | |
"ood.unity.rc.umass.edu.", | |
"docs.unity.rc.umass.edu.", | |
] | |
def dig_query(name, nameserver="8.8.8.8"): | |
try: | |
result = subprocess.check_output(["dig", "+short", f"@{nameserver}", name]) | |
return [x.strip() for x in result.decode().split('\n') if x] | |
except subprocess.CalledProcessError: | |
return [] | |
# https://stackoverflow.com/a/81899 | |
def is_ipv4_addr(x: str) -> bool: | |
try: | |
socket.inet_pton(socket.AF_INET, x) | |
return True | |
except socket.error: | |
return False | |
# https://stackoverflow.com/a/81899 | |
def is_ipv6_addr(x: str) -> bool: | |
try: | |
socket.inet_pton(socket.AF_INET6, x) | |
return True | |
except socket.error: | |
return False | |
def is_ip_addr(x: str) -> bool: | |
return is_ipv4_addr(x) or is_ipv6_addr(x) | |
def mapping2zone_file(name: str, result: str) -> str: | |
if is_ipv4_addr(result): | |
return f"{name} IN A {result}" | |
if is_ipv6_addr(result): | |
return f"{name} IN AAAA {result}" | |
return f"{name} IN CNAME {result}" | |
for lookup in LOOKUPS: | |
results = dig_query(lookup) | |
if len(results) == 0: | |
print(f"NXDOMAIN {lookup}", file=sys.stderr) | |
continue | |
# if any one of the results is not an IP address, then delete all non IP address results | |
if not all([is_ip_addr(x) for x in results]): | |
results = [x for x in results if not is_ip_addr(x)] | |
for result in results: | |
print(mapping2zone_file(lookup, result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment