diff --git a/satori/discovery.py b/satori/discovery.py index 7866445..22ae95a 100644 --- a/satori/discovery.py +++ b/satori/discovery.py @@ -23,8 +23,6 @@ Example usage: from __future__ import print_function -import socket - from novaclient.v1_1 import client import six @@ -32,39 +30,10 @@ from satori import dns from satori import utils -def is_valid_ipv4_address(address): - """Check if the address supplied is a valid IPv4 address.""" - try: - socket.inet_pton(socket.AF_INET, address) - except AttributeError: # no inet_pton here, sorry - try: - socket.inet_aton(address) - except socket.error: - return False - return address.count('.') == 3 - except socket.error: # not a valid address - return False - return True - - -def is_valid_ipv6_address(address): - """Check if the address supplied is a valid IPv6 address.""" - try: - socket.inet_pton(socket.AF_INET6, address) - except socket.error: # not a valid address - return False - return True - - -def is_valid_ip_address(address): - """Check if the address supplied is a valid IP address.""" - return is_valid_ipv4_address(address) or is_valid_ipv6_address(address) - - def run(address, config, interactive=False): """Run discovery and return results.""" results = {} - if is_valid_ip_address(address): + if utils.is_valid_ip_address(address): ipaddress = address else: ipaddress = dns.resolve_hostname(address) diff --git a/satori/utils.py b/satori/utils.py index bae65e8..317ca51 100644 --- a/satori/utils.py +++ b/satori/utils.py @@ -18,6 +18,7 @@ import datetime import logging +import socket import sys import time @@ -78,3 +79,32 @@ def parse_time_string(time_string): offset = dt_with_tz.tzinfo.utcoffset(dt_with_tz) result = dt_with_tz + offset return result.replace(tzinfo=None) + + +def is_valid_ipv4_address(address): + """Check if the address supplied is a valid IPv4 address.""" + try: + socket.inet_pton(socket.AF_INET, address) + except AttributeError: # no inet_pton here, sorry + try: + socket.inet_aton(address) + except socket.error: + return False + return address.count('.') == 3 + except socket.error: # not a valid address + return False + return True + + +def is_valid_ipv6_address(address): + """Check if the address supplied is a valid IPv6 address.""" + try: + socket.inet_pton(socket.AF_INET6, address) + except socket.error: # not a valid address + return False + return True + + +def is_valid_ip_address(address): + """Check if the address supplied is a valid IP address.""" + return is_valid_ipv4_address(address) or is_valid_ipv6_address(address)