1

I'm looking for a python replacement for nslookup.

Other sources have pointed me towards socket.getaddrinfo(). However, this does not seem to allow me to specify a server through which to resolve a hostname, which nslookup supports and I require.

C:\Users\Administrator>nslookup 10.0.11.6 coolserver
Server:  coolserv.coolserver.com
Address:  10.0.1.1

Name:    the-host-name-i-want.blah.com
Address:  10.0.11.6

It is a requirement that I be able to do the lookup through "coolserver". Is this something socket or any other python library is capable of?

I'm aware that I can just call nslookup directly through subprocess.

EDIT:

As explained above, this is not a duplicate of python module for nslookup

To my knowledge, socket.getaddrinfo() does not allow you to route the request through a server. I need to know "what does this server think my hostname is". Not "what is my local hostname".

Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78
  • Possible duplicate of [python module for nslookup](https://stackoverflow.com/questions/12297500/python-module-for-nslookup) – Aurora Wang Nov 20 '18 at 20:21
  • 1
    Not a duplicate. As I said in the OP, `socket.getaddrinfo` does not allow you to specify a server, which is the functionality I need. – Daniel Paczuski Bak Nov 20 '18 at 20:27
  • 1
    Are you looking for [dnspython](http://www.dnspython.org/) ? – Maurice Meyer Nov 20 '18 at 20:45
  • Possible duplicate of [Set specific DNS server using dns.resolver (pythondns)](https://stackoverflow.com/questions/3898363/set-specific-dns-server-using-dns-resolver-pythondns) – wesinat0r May 14 '19 at 20:28

1 Answers1

2

You need to create a resolver object and set the resolvers to the DNS servers you want to use:

from dns import *
resolver = resolver.Resolver()
resolver.nameservers = ['8.8.8.8']
a = resolver.query('duckduckgo.com','A')
a.rrset.items[0].address #'54.241.2.241'

To do a reverse lookup do this:

r =reversename.from_address('50.18.200.106')
ra = resolver.query(r,'PTR')
ra.rrset.items[0].to_text() # 'ec2-50-18-200-106.us-west-1.compute.amazonaws.com.'

Note ec2-50-18-200-106.us-west-1.compute.amazonaws.com is a duckduckgo.com web server.

You may need to install dnspython.

Red Cricket
  • 9,762
  • 21
  • 81
  • 166