Move IP testing functions to satori/utils.py
Other modules could want access to these functions in the future and this cleans up satori/discover.py. Change-Id: I6954e073c16150e90525218d1d8a15525db70110
This commit is contained in:
parent
f6141a6773
commit
6441192a98
|
@ -23,8 +23,6 @@ Example usage:
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import socket
|
|
||||||
|
|
||||||
from novaclient.v1_1 import client
|
from novaclient.v1_1 import client
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
@ -32,39 +30,10 @@ from satori import dns
|
||||||
from satori import utils
|
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):
|
def run(address, config, interactive=False):
|
||||||
"""Run discovery and return results."""
|
"""Run discovery and return results."""
|
||||||
results = {}
|
results = {}
|
||||||
if is_valid_ip_address(address):
|
if utils.is_valid_ip_address(address):
|
||||||
ipaddress = address
|
ipaddress = address
|
||||||
else:
|
else:
|
||||||
ipaddress = dns.resolve_hostname(address)
|
ipaddress = dns.resolve_hostname(address)
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
@ -78,3 +79,32 @@ def parse_time_string(time_string):
|
||||||
offset = dt_with_tz.tzinfo.utcoffset(dt_with_tz)
|
offset = dt_with_tz.tzinfo.utcoffset(dt_with_tz)
|
||||||
result = dt_with_tz + offset
|
result = dt_with_tz + offset
|
||||||
return result.replace(tzinfo=None)
|
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)
|
||||||
|
|
Loading…
Reference in New Issue