diff --git a/scripts/hw-diags.py b/scripts/hw-diags.py old mode 100644 new mode 100755 index eba99820..e3719875 --- a/scripts/hw-diags.py +++ b/scripts/hw-diags.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 """Wizard Kit: Hardware Diagnostics""" # vim: sts=2 sw=2 ts=2 diff --git a/scripts/wk/hw/diags.py b/scripts/wk/hw/diags.py index 347640d5..0cc4f42f 100644 --- a/scripts/wk/hw/diags.py +++ b/scripts/wk/hw/diags.py @@ -1231,7 +1231,11 @@ def network_test(): LOG.info('Network Test') try_and_print = std.TryAndPrint() result = try_and_print.run( - 'Network connection...', net.connected_to_private_network, msg_good='OK') + message='Network connection...', + function=net.connected_to_private_network, + msg_good='OK', + raise_on_error=True, + ) # Bail if not connected if result['Failed']: diff --git a/scripts/wk/net.py b/scripts/wk/net.py index d63d606c..c0e967ef 100644 --- a/scripts/wk/net.py +++ b/scripts/wk/net.py @@ -23,21 +23,35 @@ REGEX_VALID_IP = re.compile( # Functions -def connected_to_private_network(): - """Check if connected to a private network. +def connected_to_private_network(raise_on_error=False): + """Check if connected to a private network, returns bool. This checks for a valid private IP assigned to this system. - If one isn't found then an exception is raised. + + NOTE: If one isn't found and raise_on_error=True then an exception is raised. + NOTE 2: If one is found and raise_on_error=True then None is returned. """ + connected = False + + # Check IPs devs = psutil.net_if_addrs() for dev in devs.values(): for family in dev: if REGEX_VALID_IP.search(family.address): # Valid IP found - return + connected = True + break + if connected: + break # No valid IP found - raise GenericError('Not connected to a network') + if not connected and raise_on_error: + raise GenericError('Not connected to a network') + + # Done + if raise_on_error: + connected = None + return connected def mount_backup_shares(read_write=False): @@ -88,6 +102,10 @@ def mount_network_share(details, mount_point=None, read_write=False): username = details['RW-User'] password = details['RW-Pass'] + # Network check + if not connected_to_private_network(): + raise RuntimeError('Not connected to a network') + # Build OS-specific command if platform.system() == 'Darwin': cmd = [