Python: Reverse DNS Lookup in a shared hosting

4m1nh4j1 picture 4m1nh4j1 · Nov 8, 2013 · Viewed 16.1k times · Source

Is there any way to do a reverse lookup using python, to check the list of websites sharing the same IP address in a shared hosting.

Some web sites offer a tool for this purpose .

Answer

Kyle Kelley picture Kyle Kelley · Nov 8, 2013

DNSPython

Technically, you can use DNSPython to do a reverse lookup.

Pip install it

$ pip install dnspython

Then do your reverse query:

>>> from dns import resolver
>>> from dns import reversename
>>> addr = reversename.from_address("74.125.227.114")
>>> resolver.query(addr, "PTR")[0]
<DNS IN PTR rdata: dfw06s16-in-f18.1e100.net.>

socket.gethostbyaddr

You can also use socket.gethostbyaddr

>>> import socket
>>> name, alias, addresslist = socket.gethostbyaddr('192.30.252.130')
>>> name
'ip1c-lb3-prd.iad.github.com'

Note that you'll want to check for a socket.herror Exception when using gethostbyaddr.

Problems with doing a reverse lookup

As for finding out what sites are hosted on a particular IP, this may not lend the best results in a shared hosting environment. It will likely tell you about the provider, not the site:

14:38:43 ~/code/tmp$ ping mozeyondown.com
PING mozeyondown.com (173.203.99.161): 56 data bytes
64 bytes from 173.203.99.161: icmp_seq=0 ttl=56 time=40.924 ms

Let's look up that address now

14:38:54 ~/code/tmp$ dig +noall +answer -x 173.203.99.161
161.99.203.173.in-addr.arpa. 86053 IN   PTR 173-203-99-161.static.cloud-ips.com.

Looking it up via Python

>>> import socket
>>> name, alias, addresslist = socket.gethostbyaddr('173.203.99.161')
>>> name
'173-203-99-161.static.cloud-ips.com'

Same goes for using DNSPython.