example1b.rst revision 238106
1238106Sdes.. _example_reverse_lookup: 2238106Sdes 3238106Sdes============================== 4238106SdesReverse DNS lookup 5238106Sdes============================== 6238106Sdes 7238106SdesReverse DNS lookup involves determining the hostname associated with a given IP address. 8238106SdesThis example shows how reverse lookup can be done using unbound module. 9238106Sdes 10238106SdesFor the reverse DNS records, the special domain in-addr.arpa is reserved. 11238106SdesFor 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. 12238106Sdes 13238106Sdes:: 14238106Sdes 15238106Sdes #!/usr/bin/python 16238106Sdes import unbound 17238106Sdes 18238106Sdes ctx = unbound.ub_ctx() 19238106Sdes ctx.resolvconf("/etc/resolv.conf") 20238106Sdes 21238106Sdes status, result = ctx.resolve(unbound.reverse("74.125.43.147") + ".in-addr.arpa.", unbound.RR_TYPE_PTR, unbound.RR_CLASS_IN) 22238106Sdes if status == 0 and result.havedata: 23238106Sdes print "Result.data:", result.data.domain_list 24238106Sdes elif status != 0: 25238106Sdes print "Resolve error:", unbound.ub_strerror(status) 26238106Sdes 27238106SdesIn order to simplify the python code, unbound module contains function which reverses the hostname components. 28238106SdesThis function is defined as follows:: 29238106Sdes 30238106Sdes def reverse(domain): 31238106Sdes return '.'.join([a for a in domain.split(".")][::-1]) 32238106Sdes 33238106Sdes 34