New hw-diags-network script

This commit is contained in:
2Shirt 2017-12-22 00:45:42 -07:00
parent 1760c030ef
commit a1b72c0aeb
6 changed files with 69 additions and 80 deletions

View file

@ -17,8 +17,7 @@ if __name__ == '__main__':
clear_screen()
# Connect
if not is_connected():
connect_to_network()
connect_to_network()
# Done
print_standard('\nDone.')

View file

@ -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:

View file

@ -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'])

45
.bin/Scripts/hw-diags-network Executable file
View file

@ -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()

View file

@ -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}'

View file

@ -18,8 +18,7 @@ if __name__ == '__main__':
clear_screen()
# Connect
if not is_connected():
connect_to_network()
connect_to_network()
# Mount
if is_connected():