Skip to content

Instantly share code, notes, and snippets.

@mlow
Last active March 5, 2024 19:52
Show Gist options
  • Select an option

  • Save mlow/a9c90b54b06c45241e8a20d4719f705f to your computer and use it in GitHub Desktop.

Select an option

Save mlow/a9c90b54b06c45241e8a20d4719f705f to your computer and use it in GitHub Desktop.
import subprocess
import json
def run_axfr_query(name_server, domain):
# Run the AXFR query using dig
command = f"dig @{name_server} {domain} AXFR +noall +answer"
output = subprocess.check_output(command, shell=True, universal_newlines=True)
# Parse the output and create the JSON dictionary
records = {}
for line in output.strip().split("\n"):
parts = line.split()
name = parts[0]
record_type = parts[3]
value = " ".join(parts[4:])
if name not in records:
records[name] = {}
if record_type not in records[name]:
records[name][record_type] = []
records[name][record_type].append(value)
return json.dumps(records, indent=4)
# Example usage
name_server = "ns1.example.com" # or sys.argv[1]
domain = "example.com" # or sys.argv[2]
json_output = run_axfr_query(name_server, domain)
print(json_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment