diff --git a/.bin/Scripts/connect-to-network b/.bin/Scripts/connect-to-network index 9f388581..0475c15c 100755 --- a/.bin/Scripts/connect-to-network +++ b/.bin/Scripts/connect-to-network @@ -17,8 +17,7 @@ if __name__ == '__main__': clear_screen() # Connect - if not is_connected(): - connect_to_network() + connect_to_network() # Done print_standard('\nDone.') diff --git a/.bin/Scripts/functions/common.py b/.bin/Scripts/functions/common.py index 9cd52f4b..a58fa5ce 100644 --- a/.bin/Scripts/functions/common.py +++ b/.bin/Scripts/functions/common.py @@ -459,10 +459,13 @@ def try_and_print(message='Trying...', try: out = function(*args, **kwargs) if print_return: - print_standard(out[0], timestamp=False) - for item in out[1:]: + str_list = out + if isinstance(out, subprocess.CompletedProcess): + str_list = out.stdout.decode().strip().splitlines() + print_standard(str_list[0].strip(), timestamp=False) + for item in str_list[1:]: print_standard('{indent}{item}'.format( - indent=' '*(indent+width), item=item)) + indent=' '*(indent+width), item=item.strip())) elif silent_function: print_success(cs, timestamp=False) except w_exceptions as e: diff --git a/.bin/Scripts/functions/network.py b/.bin/Scripts/functions/network.py index 6b8c2c3c..1f987248 100644 --- a/.bin/Scripts/functions/network.py +++ b/.bin/Scripts/functions/network.py @@ -53,6 +53,22 @@ def is_connected(): # Else return False +def show_valid_addresses(): + devs = psutil.net_if_addrs() + for dev, families in sorted(devs.items()): + for family in families: + if REGEX_VALID_IP.search(family.address): + # Valid IP found + show_data(message=dev, data=family.address) + +def speedtest(): + result = run_program(['speedtest-cli', '--simple']) + output = [line.strip() for line in result.stdout.decode().splitlines() + if line.strip()] + output = [line.split() for line in output] + output = [(a, float(b), c) for a, b, c in output] + return ['{:10}{:6.2f} {}'.format(*line) for line in output] + def reload_tg3(): """Reload tg3 module as a workaround for some Dell systems.""" run_program(['sudo', 'modprobe', '-r', 'tg3']) diff --git a/.bin/Scripts/hw-diags-network b/.bin/Scripts/hw-diags-network new file mode 100755 index 00000000..b542d739 --- /dev/null +++ b/.bin/Scripts/hw-diags-network @@ -0,0 +1,45 @@ +#!/bin/python3 +# +## Wizard Kit: HW Diagnostics - Network + +import os +import sys + +# Init +os.chdir(os.path.dirname(os.path.realpath(__file__))) +sys.path.append(os.getcwd()) +from functions.network import * + +def check_connection(): + if not is_connected(): + # Raise to cause NS in try_and_print() + raise Exception + +if __name__ == '__main__': + try: + # Prep + clear_screen() + + # Connect + print_standard('Initializing...') + connect_to_network() + + # Tests + try_and_print( + message='Network connection:', function=check_connection, cs='OK') + show_valid_addresses() + try_and_print(message='Internet connection:', function=ping, + addr='8.8.8.8', cs='OK') + try_and_print(message='DNS Resolution:', function=ping, cs='OK') + try_and_print(message='Speedtest:', function=speedtest, + print_return=True) + + # Done + print_standard('\nDone.') + #pause("Press Enter to exit...") + exit_script() + except SystemExit: + pass + except: + major_exception() + diff --git a/.bin/Scripts/linux-old/hw-diags-network b/.bin/Scripts/linux-old/hw-diags-network deleted file mode 100755 index 997a8f0c..00000000 --- a/.bin/Scripts/linux-old/hw-diags-network +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/bash -# -## Wizard Kit: HW Diagnostics - Network - -function test_connection() { - cmd="a" - if [[ -e "/sys/class/net/$1" ]]; then - cmd="a show $1" - fi - if ip $cmd | grep -Eq '(10.[0-9]+|172.(1[6-9]|2[0-9]|3[0-1]).[0-9]+|192.168).[0-9]+.[0-9]+'; then - return 0 - else - return 1 - fi -} - - -CLEAR="\e[0m" -RED="\e[31m" -GREEN="\e[32m" -YELLOW="\e[33m" -BLUE="\e[34m" - -# Header -echo "WK HW Diagnostics - Network" -echo "" - -# Start Wifi if necessary -echo "Initializing..." -connect-to-network >/dev/null 2>&1 - -# Check network connection -echo -n "Network connection: " -if test_connection; then - echo -e "${GREEN}OK${CLEAR}" -else - echo -e "${RED}No access${CLEAR}" - exit 1 -fi - -# Check IP addresses -for d in /sys/class/net/*; do - device="$(basename $d)" - if [ "$device" != "lo" ]; then - if test_connection $device; then - ip="$(ip a show $device | egrep 'inet [0-9]' | sed -r 's#.*inet (.*?/[0-9]+).*#\1#')" - echo "$device: $ip" | awk '{printf " %-16s %s\n", $1, $2}' - fi - fi -done - -# Check internet connection -echo -n "Internet connection: " -if ping -c 2 -q 8.8.8.8 >/dev/null 2>&1; then - echo -e "${GREEN}OK${CLEAR}" -else - echo -e "${RED}No access${CLEAR}" - exit 1 -fi - -# Check DNS -echo -n "DNS Resolution: " -if ping -c 2 -q google.com >/dev/null 2>&1; then - echo -e "${GREEN}OK${CLEAR}" -else - echo -e "${RED}Unable to resolve google.com${CLEAR}" - exit 1 -fi - -# Check speed -echo "Speedtest:" -speedtest-cli --simple | awk '{printf " %-16s %6.2f %s\n", $1, $2, $3}' - diff --git a/.bin/Scripts/mount-backup-shares b/.bin/Scripts/mount-backup-shares index 1185c9b7..9706a0a5 100755 --- a/.bin/Scripts/mount-backup-shares +++ b/.bin/Scripts/mount-backup-shares @@ -18,8 +18,7 @@ if __name__ == '__main__': clear_screen() # Connect - if not is_connected(): - connect_to_network() + connect_to_network() # Mount if is_connected():