Skip to content

Instantly share code, notes, and snippets.

@bodsch
Last active April 18, 2023 10:25
Show Gist options
  • Save bodsch/938d2c12ea75e68a43b3cdaff59aa9bd to your computer and use it in GitHub Desktop.
Save bodsch/938d2c12ea75e68a43b3cdaff59aa9bd to your computer and use it in GitHub Desktop.
python script for DNS resolve
#!/usr/bin/env python
import dns.exception
# from dns.exception import DNSExcept
from dns.resolver import Resolver
def dns_lookup(input, timeout=3, server=[]):
"""
Perform a simple DNS lookup, return results in a dictionary
"""
resolver = Resolver()
resolver.timeout = float(timeout)
resolver.lifetime = float(timeout)
result = {}
if server:
resolver.nameservers = server
try:
records = resolver.resolve(input)
result = {
"addrs": [ii.address for ii in records],
"error": "",
"name": input,
}
except dns.resolver.NXDOMAIN as e:
result = {
"addrs": [],
"error": f"No such domain {input}",
"name": input,
}
except dns.resolver.Timeout:
result = {
"addrs": [],
"error": f"Timed out while resolving {input}",
"name": input,
}
# print(f"Timed out while resolving {input}")
except dns.exception.DNSException as e:
result = {
"addrs": [],
"error": f"Unhandled exception ({repr(e)})",
"name": input,
}
# print("Unhandled exception")
return result
for d in ["www.boone-schulz.de", "my-route.hell.com", "my-route.hell.com.local", "satellite"]:
print(dns_lookup(d)) # ,server=["8.8.8.8"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment