13b6c3722Schristos.. _example_reverse_lookup: 23b6c3722Schristos 33b6c3722SchristosReverse DNS lookup 4*0cd9f4ecSchristos================== 53b6c3722Schristos 6*0cd9f4ecSchristosReverse DNS lookup involves determining the hostname associated with a given IP 7*0cd9f4ecSchristosaddress. 83b6c3722SchristosThis example shows how reverse lookup can be done using unbound module. 93b6c3722Schristos 103b6c3722SchristosFor the reverse DNS records, the special domain in-addr.arpa is reserved. 11*0cd9f4ecSchristosFor example, a host name for the IP address ``74.125.43.147`` can be obtained 12*0cd9f4ecSchristosby issuing a DNS query for the PTR record for address 13*0cd9f4ecSchristos``147.43.125.74.in-addr.arpa.`` 14*0cd9f4ecSchristos 15*0cd9f4ecSchristosSource code 16*0cd9f4ecSchristos----------- 173b6c3722Schristos 183b6c3722Schristos:: 193b6c3722Schristos 203b6c3722Schristos #!/usr/bin/python 213b6c3722Schristos import unbound 223b6c3722Schristos 233b6c3722Schristos ctx = unbound.ub_ctx() 243b6c3722Schristos ctx.resolvconf("/etc/resolv.conf") 253b6c3722Schristos 263b6c3722Schristos status, result = ctx.resolve(unbound.reverse("74.125.43.147") + ".in-addr.arpa.", unbound.RR_TYPE_PTR, unbound.RR_CLASS_IN) 273b6c3722Schristos if status == 0 and result.havedata: 283b6c3722Schristos print "Result.data:", result.data.domain_list 293b6c3722Schristos elif status != 0: 303b6c3722Schristos print "Resolve error:", unbound.ub_strerror(status) 313b6c3722Schristos 32*0cd9f4ecSchristosIn order to simplify the python code, unbound module contains the 33*0cd9f4ecSchristos:meth:`unbound.reverse` function which reverses the hostname components. 343b6c3722SchristosThis function is defined as follows:: 353b6c3722Schristos 363b6c3722Schristos def reverse(domain): 373b6c3722Schristos return '.'.join([a for a in domain.split(".")][::-1]) 38