Updated network.py

This commit is contained in:
2Shirt 2018-12-27 19:58:06 -07:00
parent 097fae866a
commit 018aba2fe6
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -13,60 +13,61 @@ from functions.common import *
# REGEX # REGEX
REGEX_VALID_IP = re.compile( REGEX_VALID_IP = re.compile(
r'(10.\d+.\d+.\d+' r'(10.\d+.\d+.\d+'
r'|172.(1[6-9]|2\d|3[0-1])' r'|172.(1[6-9]|2\d|3[0-1])'
r'|192.168.\d+.\d+)', r'|192.168.\d+.\d+)',
re.IGNORECASE) re.IGNORECASE)
def connect_to_network(): def connect_to_network():
"""Connect to network if not already connected.""" """Connect to network if not already connected."""
net_ifs = psutil.net_if_addrs() net_ifs = psutil.net_if_addrs()
net_ifs = [i[:2] for i in net_ifs.keys()] net_ifs = [i[:2] for i in net_ifs.keys()]
# Bail if currently connected # Bail if currently connected
if is_connected(): if is_connected():
return return
# WiFi # WiFi
if 'wl' in net_ifs: if 'wl' in net_ifs:
cmd = [ cmd = [
'nmcli', 'dev', 'wifi', 'nmcli', 'dev', 'wifi',
'connect', WIFI_SSID, 'connect', WIFI_SSID,
'password', WIFI_PASSWORD] 'password', WIFI_PASSWORD]
try_and_print( try_and_print(
message = 'Connecting to {}...'.format(WIFI_SSID), message = 'Connecting to {}...'.format(WIFI_SSID),
function = run_program, function = run_program,
cmd = cmd) cmd = cmd)
def is_connected(): def is_connected():
"""Check for a valid private IP.""" """Check for a valid private IP."""
devs = psutil.net_if_addrs() devs = psutil.net_if_addrs()
for dev in devs.values(): for dev in devs.values():
for family in dev: for family in dev:
if REGEX_VALID_IP.search(family.address): if REGEX_VALID_IP.search(family.address):
# Valid IP found # Valid IP found
return True return True
# Else # Else
return False return False
def show_valid_addresses(): def show_valid_addresses():
"""Show all valid private IP addresses assigned to the system.""" """Show all valid private IP addresses assigned to the system."""
devs = psutil.net_if_addrs() devs = psutil.net_if_addrs()
for dev, families in sorted(devs.items()): for dev, families in sorted(devs.items()):
for family in families: for family in families:
if REGEX_VALID_IP.search(family.address): if REGEX_VALID_IP.search(family.address):
# Valid IP found # Valid IP found
show_data(message=dev, data=family.address) show_data(message=dev, data=family.address)
def speedtest(): def speedtest():
"""Run a network speedtest using speedtest-cli.""" """Run a network speedtest using speedtest-cli."""
result = run_program(['speedtest-cli', '--simple']) result = run_program(['speedtest-cli', '--simple'])
output = [line.strip() for line in result.stdout.decode().splitlines() output = [line.strip() for line in result.stdout.decode().splitlines()
if line.strip()] if line.strip()]
output = [line.split() for line in output] output = [line.split() for line in output]
output = [(a, float(b), c) for a, b, c in output] output = [(a, float(b), c) for a, b, c in output]
return ['{:10}{:6.2f} {}'.format(*line) for line in output] return ['{:10}{:6.2f} {}'.format(*line) for line in output]
if __name__ == '__main__': if __name__ == '__main__':
print("This file is not meant to be called directly.") print("This file is not meant to be called directly.")
# vim: sts=2 sw=2 ts=2