Python: Reverse DNS Lookup in a shared hosting

后端 未结 1 1235
悲&欢浪女
悲&欢浪女 2021-02-10 13:51

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

1条回答
  •  孤独总比滥情好
    2021-02-10 14:21

    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]
    
    

    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.

    0 讨论(0)
提交回复
热议问题