Build a Python DNS Lookup Tool: Inspect Any Domain Like a Pro
Security professionals inspect domains all day — checking mail records, spotting misconfigurations, verifying DNSSEC. Doing it one command at a time is slow. Today we’ll build a single Python tool that pulls a domain’s key DNS records in one run. It only reads public DNS data, so it’s completely defensive: use it on your own domains, or on domains you have permission to assess.
Setup
Install the dnspython library:
pip install dnspython
The Code
import dns.resolverimport sysRECORD_TYPES = ["A", "AAAA", "MX", "NS", "TXT", "CNAME", "SOA"]def lookup(domain, rtype): """Return a list of answers for one record type, or an empty list.""" try: answers = dns.resolver.resolve(domain, rtype, lifetime=5) return [a.to_text() for a in answers] except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.NoNameservers, dns.exception.Timeout): return []def main(): if len(sys.argv) < 2: print("Usage: python3 dnslookup.py example.com") sys.exit(1) domain = sys.argv[1].strip() print(f"\nDNS report for: {domain}\n" + "=" * (16 + len(domain))) for rtype in RECORD_TYPES: results = lookup(domain, rtype) print(f"\n[{rtype}]") if results: for r in results: print(f" {r}") else: print(" (no records)")if __name__ == "__main__": main()
How It Works
- dns.resolver.resolve() queries public DNS for one record type at a time, with a 5-second timeout so a dead record doesn’t hang the script.
- Each record type gets its own error handling — a missing MX record shouldn’t crash the whole report.
- The output is grouped by record type, so you can scan it at a glance.
What Each Record Tells You
- A / AAAA — the domain’s IPv4 and IPv6 addresses. Multiple A records often mean a CDN or load balancer.
- MX — mail servers. Missing MX records on a domain that sends email is a misconfiguration worth flagging.
- NS — authoritative nameservers. Unexpected nameservers can indicate a DNS hijack.
- TXT — SPF, DKIM and DMARC records live here. If a domain sends email but has no SPF record, spoofing it is trivially easy.
- CNAME — aliases pointing to other domains. Stale CNAMEs pointing at abandoned services are a classic subdomain-takeover risk.
- SOA — the start-of-authority record, naming the primary nameserver and the zone’s serial number.
Try It
python3 dnslookup.py example.com
python3 dnslookup.py your-own-domain.com
Run it against your own domain first. Then check: does your domain have SPF and DMARC TXT records? Are the nameservers the ones you expect? Those two checks alone catch real misconfigurations.
Level Up
- Add a flag to check whether SPF/DMARC records exist and warn if they’re missing.
- Compare results against a saved baseline and alert when records change — that’s a poor man’s DNS monitoring.
- Resolve through a specific resolver (like 1.1.1.1) by setting resolver.nameservers = [“1.1.1.1”].
You just built a tool that security teams use daily. Keep building defensive tools, and the offensive knowledge follows naturally — on your own systems, where it belongs.



Comments
Post a Comment