Reverse NS Lookup

Matthew Dixon Cowles matt at mondoinfo.com
Sun Oct 22 14:29:56 EDT 2000


On Sun, 22 Oct 2000 17:43:03 GMT, Simon Faulkner <News at Titanic.co.uk>
wrote:

>How do I find the domain name of an IP address?

If you're running under Unix, you could open a pipe from nslookup (or
another similar command line tool) and parse the result:

import os
import string

def main():
  p=os.popen("/usr/bin/nslookup 208.185.174.123")

  while 1:
    l=p.readline()
    if l[:6]=="Name: ":
      domainName=string.strip(l[6:])
      break
  p.close()
  print domainName

While that's not hard, it leaves you vulnerable to a change in
nslookup's output format.

Alternatively, you can prolly use the dns code that's in the source
distribution's Demo directory. I've been using Anthony Baxter's
improved version of that code. You can find information about it at:

http://www.vex.net/parnassus/apyllo.py?i=632080673

If you're using that code, all you need to do is:

>>> import DNS
>>> DNS.ParseResolvConf()
>>> DNS.revlookup("208.185.174.123")

You should be aware that a very small number of evil spammers
deliberately put false information in their reverse lookup databases
and that a fair number of clueless admins don't put any data in them.

Regards,
Matt



More information about the Python-list mailing list