Skip to content

Commit

Permalink
Remove deprecated methods / functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenning committed Nov 29, 2023
1 parent 090ce75 commit cb7f6ab
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 152 deletions.
3 changes: 0 additions & 3 deletions ciscoconfparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@
from ciscoconfparse.ccp_util import L4Object
from ciscoconfparse.ccp_util import DNSResponse
from ciscoconfparse.ccp_util import dns_query
from ciscoconfparse.ccp_util import dns_lookup
from ciscoconfparse.ccp_util import dns6_lookup
from ciscoconfparse.ccp_util import check_valid_ipaddress
from ciscoconfparse.ccp_util import reverse_dns_lookup

from dns.resolver import Resolver
from dns.exception import DNSException
Expand Down
120 changes: 0 additions & 120 deletions ciscoconfparse/ccp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2802,92 +2802,6 @@ def dns_query(input_str="", query_type="", server="", timeout=2.0):
return retval


@logger.catch(reraise=True)
@deprecated(reason="dns_lookup() is obsolete; use dns_query() instead. dns_lookup() will be removed", version='1.7.0')
def dns_lookup(input_str, timeout=3, server="", record_type="A"):
"""Perform a simple DNS lookup, return results in a dictionary"""
if not isinstance(input_str, str):
raise ValueError

if not isinstance(timeout, (int, float)):
error = f"An integer is required for `timeout`; not {timeout} {type(timeout)}"
logger.error(error)
raise DNSTimeoutError(error)

if not isinstance(server, str):
error = f"An string is required for `server`; not {server} {type(server)}"
logger.error(error)
raise DNSTimeoutError(error)

if not isinstance(record_type, str):
error = f"An string is required for `record_type`; not {record_type} {type(record_type)}."
logger.error(error)
raise DNSTimeoutError(error)

rr = Resolver()
rr.timeout = float(timeout)
rr.lifetime = float(timeout)
if server != "":
rr.nameservers = [server]
# dns_session = rr.resolve(input_str, record_type)
dns_session = rr.query(input_str, record_type)
responses = list()
for rdata in dns_session:
responses.append(str(rdata))

"""
from dns import resolver
rr = resolver.Resolver()
rr.nameservers = ['8.8.8.8']
rr.timeout = 2.0
foo = rr.resolve('cisco.com', 'A')
for rdata in foo:
print("ADDR", rdata)
"""

try:
return {
"record_type": record_type,
"addrs": responses,
"error": "",
"name": input_str,
}
except DNSException as e:
return {
"record_type": record_type,
"addrs": [],
"error": repr(e),
"name": input_str,
}


@logger.catch(reraise=True)
@deprecated(reason="dns6_lookup() is obsolete; use dns_query() instead. dns6_lookup() will be removed", version='1.7.0')
def dns6_lookup(input_str, timeout=3, server=""):
"""Perform a simple DNS lookup, return results in a dictionary"""
rr = Resolver()
rr.timeout = float(timeout)
rr.lifetime = float(timeout)
if server:
rr.nameservers = [server]
try:
records = rr.query(input_str, "AAAA")
return {
"addrs": [ii.address for ii in records],
"error": "",
"name": input_str,
}
except DNSException as e:
return {
"addrs": [],
"error": repr(e),
"name": input_str,
}


_REVERSE_DNS_REGEX = re.compile(r"^\s*\d+\.\d+\.\d+\.\d+\s*$")


@logger.catch(reraise=True)
def check_valid_ipaddress(input_addr=None):
"""
Expand Down Expand Up @@ -2921,40 +2835,6 @@ def check_valid_ipaddress(input_addr=None):
return (input_addr, ipaddr_family)


@logger.catch(reraise=True)
@deprecated(reason="reverse_dns_lookup() is obsolete; use dns_query() instead. reverse_dns_lookup() will be removed", version='1.7.0')
def reverse_dns_lookup(input_str, timeout=3.0, server="4.2.2.2", proto="udp"):
"""Perform a simple reverse DNS lookup on an IPv4 or IPv6 address; return results in a python dictionary"""
if not isinstance(proto, str) and (proto == "udp" or proto == "tcp"):
raise ValueError

if not isinstance(float(timeout), float) and float(timeout) > 0.0:
raise ValueError


addr, addr_family = check_valid_ipaddress(input_str)
assert addr_family == 4 or addr_family == 6

if proto != "tcp" and proto != "udp":
raise ValueError()

raw_result = dns_query(input_str, query_type="PTR", server=server, timeout=timeout)
if not isinstance(raw_result, set):
raise ValueError

assert len(raw_result) >= 1
tmp = raw_result.pop()
if not isinstance(tmp, DNSResponse):
raise ValueError


if tmp.has_error is True:
retval = {'addrs': [input_str], 'error': str(tmp.error_str), 'name': tmp.result_str}
else:
retval = {'addrs': [input_str], 'error': '', 'name': tmp.result_str}
return retval


class CiscoIOSInterface(object):
interface_name = None
interface_dict = None
Expand Down
30 changes: 1 addition & 29 deletions tests/test_Ccp_Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from ciscoconfparse.ccp_util import CiscoIOSInterface, CiscoIOSXRInterface
from ciscoconfparse.ccp_util import _RGX_IPV4ADDR, _RGX_IPV6ADDR
from ciscoconfparse.ccp_util import IPv6Obj, IPv4Obj, L4Object, ip_factory
from ciscoconfparse.ccp_util import dns_lookup, reverse_dns_lookup, collapse_addresses
from ciscoconfparse.ccp_util import collapse_addresses
import sys

sys.path.insert(0, "..")
Expand Down Expand Up @@ -640,34 +640,6 @@ def test_collapse_addresses_02():
assert collapsed_list[1].network_address == IPv4Obj('192.0.2.128/25').ip


def test_dns_lookup():
# Use my hostname to test...
test_hostname = "pennington.net"
result_correct_address = "65.19.187.2"
result_correct = {"addrs": [result_correct_address], "name": test_hostname, "error": "", "record_type": "A"}
try:
test_result = dns_lookup(test_hostname, timeout=0.45)
except Exception as eee:
error = f"Skipping test_dns_lookup due to Exception: {eee}"
logger.warning(error)
pytest.skip(error)

if test_result["error"] != "":
assert dns_lookup(test_hostname) == result_correct
else:
pytest.skip(test_result["error"])


def test_reverse_dns_lookup():

#result_correct = {"addrs": ["127.0.0.1"], "name": "localhost.", "error": ""}
try:
test_result = reverse_dns_lookup("127.0.0.1", timeout=0.1)
assert test_result["error"] == ""
except Exception:
pytest.skip(test_result["error"])


def test_CiscoIOSInterface_01():
"""Check that a single number is parsed correctly"""
uut = CiscoIOSInterface("Ethernet1")
Expand Down

0 comments on commit cb7f6ab

Please sign in to comment.