diff options
author | Daniel Watkins <oddbloke@ubuntu.com> | 2020-03-27 22:47:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-27 20:47:01 -0600 |
commit | 2566fdbee06cf5708cfcd988a65fb81cdf794dbf (patch) | |
tree | 3f631450c78aab545888437ad6814496c2b3a649 /cloudinit/net/__init__.py | |
parent | 36a597de6f130a17360856697759b08f3c835df1 (diff) | |
download | vyos-cloud-init-2566fdbee06cf5708cfcd988a65fb81cdf794dbf.tar.gz vyos-cloud-init-2566fdbee06cf5708cfcd988a65fb81cdf794dbf.zip |
net: introduce is_ip_address function (#288)
This will be required for the mirror URL sanitisation work,
Diffstat (limited to 'cloudinit/net/__init__.py')
-rw-r--r-- | cloudinit/net/__init__.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 67e3d578..08eaf0a3 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -6,13 +6,14 @@ # This file is part of cloud-init. See LICENSE file for license information. import errno +import ipaddress import logging import os import re from functools import partial -from cloudinit.net.network_state import mask_to_net_prefix from cloudinit import util +from cloudinit.net.network_state import mask_to_net_prefix from cloudinit.url_helper import UrlError, readurl LOG = logging.getLogger(__name__) @@ -961,6 +962,22 @@ def has_url_connectivity(url): return True +def is_ip_address(s: str) -> bool: + """Returns a bool indicating if ``s`` is an IP address. + + :param s: + The string to test. + + :return: + A bool indicating if the string contains an IP address or not. + """ + try: + ipaddress.ip_address(s) + except ValueError: + return False + return True + + class EphemeralIPv4Network(object): """Context manager which sets up temporary static network configuration. |