Query DNS server for usable/plain IP address
Question:
I am trying to write a program to query multiple DNS servers for the IP address of a hostname. My only problem is that I can’t get the actual IP address, only the server’s response.
This is my code:
import dns.resolver
my_resolver = dns.resolver.Resolver()
reply_list = []
dns_list = ['8.8.8.8', '8.8.4.4', '1.2.3.4']
resp = []
for server in dns_list:
try:
my_resolver.nameservers = [server]
answer = my_resolver.resolve('google.com')
print(answer.response)
except dns.resolver.LifetimeTimeout:
print('Error')
I don’t understand how I can use the output.
I had hoped for a list, a dict or a tuple as a response.
But I get this on the console:
id 54769
opcode QUERY
rcode NOERROR
flags QR RD RA
;QUESTION
google.com. IN A
;ANSWER
google.com. 233 IN A 172.217.16.206
;AUTHORITY
;ADDITIONAL
id 39642
opcode QUERY
rcode NOERROR
flags QR RD RA
;QUESTION
google.com. IN A
;ANSWER
google.com. 300 IN A 172.217.18.14
;AUTHORITY
;ADDITIONAL
Error
Process finished with exit code 0`
Thanks for your help! 🙂
Answers:
answer.response
contains the actual DNS protocol response from the server. Instead, you can use str(answer)
to get the resolved IP address instead. Like this:
import dns.resolver
server_list = ['8.8.8.8', '8.8.4.4']
records = []
dns.resolver.default_resolver = dns.resolver.Resolver(configure=False)
for serv in server_list:
dns.resolver.default_resolver.nameservers = [serv]
answers = dns.resolver.resolve('google.com')
records.extend([str(answer) for answer in answers])
print(records) # => ['172.217.1.110', '142.250.191.142']
Thanks for the help!
This is what I ended up doing to get a list with dicts back.
With the DNS server as the key and the result (IP-Address) as value.
Even though I still don’t understand how that server response seemingly "magically" gets turned into the plain IP-Address.
Any resource for that is appreciated! 🙂
import dns.resolver
my_resolver = dns.resolver.Resolver()
reply_list = []
dns_list = ['8.8.8.8', '8.8.4.4', '1.2.3.4']
records_dict_list = []
resp = []
for server in dns_list:
try:
my_resolver.nameservers = [server]
answers = my_resolver.resolve('google.com', 'A')
records_dict_list.append({str(server): str(answer) for answer in answers})
except dns.resolver.LifetimeTimeout:
records_dict_list.append({str(server): 'error'})
print(records_dict_list)
Outputs:
[{'8.8.8.8': '142.250.186.78'}, {'8.8.4.4': '172.217.16.206'}, {'1.2.3.4': 'error'}]
Process finished with exit code 0
I am trying to write a program to query multiple DNS servers for the IP address of a hostname. My only problem is that I can’t get the actual IP address, only the server’s response.
This is my code:
import dns.resolver
my_resolver = dns.resolver.Resolver()
reply_list = []
dns_list = ['8.8.8.8', '8.8.4.4', '1.2.3.4']
resp = []
for server in dns_list:
try:
my_resolver.nameservers = [server]
answer = my_resolver.resolve('google.com')
print(answer.response)
except dns.resolver.LifetimeTimeout:
print('Error')
I don’t understand how I can use the output.
I had hoped for a list, a dict or a tuple as a response.
But I get this on the console:
id 54769
opcode QUERY
rcode NOERROR
flags QR RD RA
;QUESTION
google.com. IN A
;ANSWER
google.com. 233 IN A 172.217.16.206
;AUTHORITY
;ADDITIONAL
id 39642
opcode QUERY
rcode NOERROR
flags QR RD RA
;QUESTION
google.com. IN A
;ANSWER
google.com. 300 IN A 172.217.18.14
;AUTHORITY
;ADDITIONAL
Error
Process finished with exit code 0`
Thanks for your help! 🙂
answer.response
contains the actual DNS protocol response from the server. Instead, you can use str(answer)
to get the resolved IP address instead. Like this:
import dns.resolver
server_list = ['8.8.8.8', '8.8.4.4']
records = []
dns.resolver.default_resolver = dns.resolver.Resolver(configure=False)
for serv in server_list:
dns.resolver.default_resolver.nameservers = [serv]
answers = dns.resolver.resolve('google.com')
records.extend([str(answer) for answer in answers])
print(records) # => ['172.217.1.110', '142.250.191.142']
Thanks for the help!
This is what I ended up doing to get a list with dicts back.
With the DNS server as the key and the result (IP-Address) as value.
Even though I still don’t understand how that server response seemingly "magically" gets turned into the plain IP-Address.
Any resource for that is appreciated! 🙂
import dns.resolver
my_resolver = dns.resolver.Resolver()
reply_list = []
dns_list = ['8.8.8.8', '8.8.4.4', '1.2.3.4']
records_dict_list = []
resp = []
for server in dns_list:
try:
my_resolver.nameservers = [server]
answers = my_resolver.resolve('google.com', 'A')
records_dict_list.append({str(server): str(answer) for answer in answers})
except dns.resolver.LifetimeTimeout:
records_dict_list.append({str(server): 'error'})
print(records_dict_list)
Outputs:
[{'8.8.8.8': '142.250.186.78'}, {'8.8.4.4': '172.217.16.206'}, {'1.2.3.4': 'error'}]
Process finished with exit code 0