get_rdns

privex.helpers.net.get_rdns(host: Union[str, ipaddress.IPv4Address, ipaddress.IPv6Address], throw=True) → Optional[str][source]

Look up the reverse DNS hostname for host and return it as a string. The host can be an IP address as a str, IPv4Address, IPv6Address - or a domain.

If a domain is passed, e.g. privex.io - then the reverse DNS will be looked up for the IP address contained in the domain’s AAAA or A records.

Toggle throw to control whether to raise exceptions on error (True), or to simply return None (False).

Basic usage:

>>> from privex.helpers import get_rdns
>>> get_rdns('185.130.44.10')
'web-se1.privex.io'
>>> get_rdns('2a07:e00::333')
'se.dns.privex.io'
>>> get_rdns('privex.io')
'web-se1.privex.io'

Error handling:

>>> get_rdns('192.168.4.5')
Traceback (most recent call last):
  File "<ipython-input-14-e1ed65295031>", line 1, in <module>
    get_rdns('192.168.4.5')
privex.helpers.exceptions.ReverseDNSNotFound: No reverse DNS records found for host '192.168.4.5':
    <class 'socket.herror'> [Errno 1] Unknown host
>>> get_rdns('non-existent-domain.example')
Traceback (most recent call last):
  File "<ipython-input-16-0d75d37a930f>", line 1, in <module>
    get_rdns('non-existent-domain.example')
privex.helpers.exceptions.InvalidHost: Host 'non-existent-domain.example' is not a valid IP address,
nor an existent domain: <class 'socket.gaierror'> [Errno 8] nodename nor servname provided, or not known
>>> repr(get_rdns('192.168.4.5', throw=False))
'None'
>>> repr(get_rdns('non-existent-domain.example', False))
'None'
Parameters
  • host (str|IPv4Address|IPv6Address) – An IPv4/v6 address, or domain to lookup reverse DNS for.

  • throw (bool) – (Default: True) When True, will raise ReverseDNSNotFound or InvalidHost when no rDNS records can be found for host, or when host is an invalid IP / non-existent domain. When False, will simply return None when host is invalid, or no rDNS records are found.

Raises
  • ReverseDNSNotFound – When throw is True and no rDNS records were found for host

  • InvalidHost – When throw is True and host is an invalid IP address or non-existent domain/hostname

Return Optional[str] rDNS

The reverse DNS hostname for host (value of PTR record)