File: example1b.rst

package info (click to toggle)
unbound 1.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,436 kB
  • sloc: ansic: 138,476; sh: 6,860; yacc: 4,259; python: 1,950; makefile: 1,881; awk: 162; perl: 158; xml: 36
file content (37 lines) | stat: -rw-r--r-- 1,178 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
.. _example_reverse_lookup:

Reverse DNS lookup
==================

Reverse DNS lookup involves determining the hostname associated with a given IP
address.
This example shows how reverse lookup can be done using unbound module.

For the reverse DNS records, the special domain in-addr.arpa is reserved. 
For example, a host name for the IP address ``74.125.43.147`` can be obtained
by issuing a DNS query for the PTR record for address
``147.43.125.74.in-addr.arpa.``

Source code
-----------

::

    #!/usr/bin/python
    import unbound

    ctx = unbound.ub_ctx()
    ctx.resolvconf("/etc/resolv.conf")

    status, result = ctx.resolve(unbound.reverse("74.125.43.147") + ".in-addr.arpa.", unbound.RR_TYPE_PTR, unbound.RR_CLASS_IN)
    if status == 0 and result.havedata:
        print "Result.data:", result.data.domain_list
    elif status != 0:
        print "Resolve error:", unbound.ub_strerror(status)

In order to simplify the python code, unbound module contains the
:meth:`unbound.reverse` function which reverses the hostname components.
This function is defined as follows::

    def reverse(domain):
        return '.'.join([a for a in domain.split(".")][::-1])