Last active
March 4, 2021 19:51
-
-
Save landonstewart/84699373f47477d3107df7a5e0afc868 to your computer and use it in GitHub Desktop.
Various dns/ip/ipwhois related functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@cache_memoize(1200) | |
def dns_lookup(hostname): | |
"""Lookup both A and AAAA. | |
Args: | |
hostname (str): Hostname that you'd like to get the ip address(es) for | |
port (int): Port against which to do the lookup | |
Returns: | |
A list of IP addresses | |
""" | |
def resolve_hostname(hostname): | |
"""Resolve a hostname to IP(s) (IPv4 and IPv6).""" | |
ipv4res = dns.resolver.Resolver(configure=False) | |
ipv6res = dns.resolver.Resolver(configure=False) | |
ipv4res.nameservers = ['8.8.8.8', '8.8.4.4'] | |
ipv6res.nameservers = ['2001:4860:4860::8888', '2001:4860:4860::8844'] | |
try: | |
for ans in ipv4res.resolve(hostname, 'A'): | |
yield ans.to_text() | |
except Exception: # pylint: disable=broad-except | |
pass | |
try: | |
for ans in ipv6res.resolve(hostname, 'AAAA'): | |
yield ans.to_text() | |
except Exception: # pylint: disable=broad-except | |
pass | |
return list(resolve_hostname(hostname)) | |
@cache_memoize(1200) | |
def lookup_asn_info(ip_addr): | |
"""Lookup ASN data from local data/ files. | |
ASN_DB and ASN_NAMES are declared above. | |
Args: | |
ip_addr (IPv4Address or IPv6Address): IP address. | |
Returns: | |
{'asn': ['1000', '2000'], | |
'country': 'US', | |
'asn_name': 'Some Company Inc.'} | |
""" | |
if isinstance(ip_addr, (IPv4Network, IPv6Network)): | |
ip_addr = str(ip_addr.network_address) | |
try: | |
asn, _ = ASN_DB.lookup(str(ip_addr)) | |
except Exception as e_msg: # pylint: disable=broad-except | |
logger.warning("Failed to lookup ASN for %s: %s", ip_addr, e_msg) | |
return None | |
try: | |
as_name, as_country = ASN_NAMES[str(asn)].rsplit(', ', 1) | |
except Exception as e_msg: # pylint: disable=broad-except | |
logger.warning("Failed to lookup ASNNAME for %s: %s", asn, e_msg) | |
return None | |
return {'asn': [asn], 'country': as_country, 'asn_name': as_name} | |
@cache_memoize(300) | |
def has_valid_tld(text): | |
"""Return True if the text contains a valid TLD. | |
Args: | |
text: The text to check. | |
Returns: | |
True or False | |
""" | |
try: | |
if get_tld(text, fix_protocol=True, fail_silently=True): | |
return True | |
except Exception as e_msg: # pylint: disable=broad-except | |
logger.warning("Failed to check tld because: %s", e_msg) | |
return None | |
return None | |
@cache_memoize(300) | |
def is_valid_ip(text): | |
"""Test if text is a valid IP address.""" | |
try: | |
ip_network(text, strict=False) | |
except Exception: # pylint: disable=broad-except | |
return False | |
if is_network_address(text): | |
return False | |
return True | |
@cache_memoize(300) | |
def is_valid_url(text): | |
"""TEst if text is a valid URL.""" | |
try: | |
urlparse(text) | |
except Exception: # pylint: disable=broad-except | |
return False | |
return True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment