diff --git a/nsupdate/main/_tests/test_dnstools.py b/nsupdate/main/_tests/test_dnstools.py index e722922..508fc46 100644 --- a/nsupdate/main/_tests/test_dnstools.py +++ b/nsupdate/main/_tests/test_dnstools.py @@ -8,7 +8,7 @@ pytestmark = pytest.mark.django_db from dns.resolver import NXDOMAIN, NoAnswer -from ..dnstools import add, delete, update, query_ns, parse_name, update_ns, SameIpError, DnsUpdateError +from ..dnstools import add, delete, update, query_ns, rev_lookup, parse_name, update_ns, SameIpError, DnsUpdateError # see also conftest.py BASEDOMAIN = 'nsupdate.info' @@ -107,6 +107,16 @@ class TestQuery(object): assert query_ns(TEST_HOST, 'AAAA') == ipv6 +class TestReverseLookup(object): + def test_rev_lookup_v4(self): + name, ip = 'google-public-dns-a.google.com', '8.8.8.8' + assert rev_lookup(ip) == name + + def test_rev_lookup_v6(self): + name, ip = 'google-public-dns-a.google.com', '2001:4860:4860::8888' + assert rev_lookup(ip) == name + + class TestUpdate(object): def test_parse1(self): host, domain = 'test', BASEDOMAIN diff --git a/nsupdate/main/dnstools.py b/nsupdate/main/dnstools.py index ae608aa..88ac247 100644 --- a/nsupdate/main/dnstools.py +++ b/nsupdate/main/dnstools.py @@ -20,6 +20,8 @@ from datetime import timedelta import logging logger = logging.getLogger(__name__) +import socket + import dns.inet import dns.name import dns.resolver @@ -181,6 +183,16 @@ def query_ns(qname, rdtype, origin=None): raise +def rev_lookup(ipaddr): + """ + do a normal reverse DNS lookup, IP to name + + :param ipaddr: ip address (str) + :return: hostname + """ + return socket.gethostbyaddr(ipaddr)[0] + + def parse_name(fqdn, origin=None): """ Parse a fully qualified domain name into a relative name