Python: Reverse DNS Lookup in a shared hosting

陌路散爱 提交于 2019-12-03 21:37:27

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!