Specify DNS server when resolving address

Asked

Viewed 671 times

2

I would like to perform something like a "nslookup" to resolve a site address through my DNS servers, because in the example below, I get only through the DNS I have on my machine.

How could I inform my server IP to resolve the Google address for example?

import socket

dns = socket.gethostbyname('www.google.com')
print(dns)
  • So, imagine that I have DNS servers of my own and want to resolve addresses like google, facebook through my DNS`s.

  • This, let’s assume that I want to resolve the www.terra.com address through google’s DNS(8.8.8.8), as I would do?

  • If you run this command on the terminal, you will understand "nslookup www.terra.com.br 8.8.8.8". With this command resolves the land address through google DNS.

  • 1

    Maybe this function will help: gethostbyname_ex('www.google.com'), See if that helps

  • No, I need to figure out a way to pass my DNS address to address "www.google.com:

1 answer

5


Natively there seems to be no way to do this.

Alternatively, you can use the module dns.resolver of the tool set dnspython.

To install on Ubuntu/Debian, via apt-get, do:

sudo apt-get install python-dnspython

If you prefer to install via pip:

sudo pip install dnspython

To specify the server, use the attribute nameservers of the object Resolver.

import dns.resolver

resolver = dns.resolver.Resolver(configure=False)

# 8.8.8.8 é o DNS público do Google
resolver.nameservers = ['8.8.8.8']

answers = resolver.query('google.com')

for rdata in answers:
    print (rdata.address)

More information:

  • 1

    Excellent, it was just specifying my`s DNS that I can test correctly. "configure=False" is for not using the DNS address on my network card, that’s it?

  • Stderr’s is back...

  • Do not recode "Pip" with "sudo" as an alternative - mainly on a system where the package exists - the package installed by PIP will conflict with the package installed by "apt" (for example, an apt package that has the package installed as PIP as a prerequisite will not work)

  • and on systems where the package does not exist (either .deb, .rpm, or on Windows as a whole) the best option is to create a virtualenv: that way the installation with Pip is local to the project and does not conflict with the system’s Python.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.