add reverse dns lookup code
This commit is contained in:
parent
9294c859db
commit
0e8f318628
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user