summaryrefslogtreecommitdiff
path: root/bin
diff options
authorPMR <pmr@pmr-lander>2017-11-09 14:45:28 +0000
committerPMR <pmr@pmr-lander>2017-11-09 14:45:28 +0000
commit95380fb6ce65d5966415e985688fffc285a5c0bb (patch)
tree672414012ed96f3280af89618b3ad8d7236f77a9 /bin
parente414333bbe052903f16b874d17595649a0b6d321 (diff)
parent754f075c2179c3e9100d3659c732e58695135550 (diff)
Merge #332116 from ~jocave/plainbox-provider-checkbox:move-wireless-jobs-to-newer-templated-version
Diffstat (limited to 'bin')
-rwxr-xr-xbin/net_driver_info41
-rwxr-xr-xbin/wifi_nmcli_test154
2 files changed, 195 insertions, 0 deletions
diff --git a/bin/net_driver_info b/bin/net_driver_info
new file mode 100755
index 0000000..ee5db46
--- /dev/null
+++ b/bin/net_driver_info
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+# Copyright 2017 Canonical Ltd.
+# All rights reserved.
+#
+# Written by:
+# Jonathan Cave <jonathan.cave@canonical.com>
+#
+# Print info about drivers we can identify automatically and also those we
+# identify in the special interest list!
+
+from pathlib import Path
+import sys
+
+# Store pairs of (interface, driver)
+driver_list = []
+
+# Find drivers in sysfs
+for interface in Path("/sys/class/net").iterdir():
+ mod_path = Path("{}/device/driver/module".format(interface))
+ if mod_path.is_symlink():
+ driver_list.append((interface.name, mod_path.resolve().name))
+
+# Add user requested modules to the list. Create "unknown" interfaces if none
+# of the identified interfaces above are using it
+for user_driver in sys.argv[1:]:
+ if Path("/sys/module/{}".format(user_driver)).exists():
+ if not any(x[1] == user_driver for x in driver_list):
+ driver_list.append(("unknown", user_driver))
+ else:
+ print("Requested driver {} not loaded\n".format(user_driver))
+
+# Produce the output
+for interface, driver in driver_list:
+ print("Interface {} using module {}".format(interface, driver))
+ sysfs_path = Path("/sys/module/{}/parameters".format(driver))
+ if sysfs_path.is_dir():
+ print(" Parameters:")
+ for path in Path(sysfs_path).iterdir():
+ if path.is_file():
+ print(" {}: {}".format(path.name, path.read_text().strip()))
+ print()
diff --git a/bin/wifi_nmcli_test b/bin/wifi_nmcli_test
new file mode 100755
index 0000000..24359c2
--- /dev/null
+++ b/bin/wifi_nmcli_test
@@ -0,0 +1,154 @@
+#!/usr/bin/env python3
+# Copyright 2017 Canonical Ltd.
+# All rights reserved.
+#
+# Written by:
+# Jonathan Cave <jonathan.cave@canonical.com>
+#
+# wireless connection tests using nmcli
+
+
+import argparse
+import functools
+import subprocess as sp
+import sys
+
+
+print = functools.partial(print, flush=True)
+
+
+def print_head(txt):
+ print("##", txt)
+
+
+def print_cmd(cmd):
+ print("+", cmd)
+
+
+def cleanup_nm_connections():
+ print_head("Cleaning up NM connections")
+ cmd = "nmcli -t -f TYPE,UUID,NAME c"
+ print_cmd(cmd)
+ output = sp.check_output(cmd, shell=True)
+ for line in output.decode(sys.stdout.encoding).splitlines():
+ type, uuid, name = line.strip().split(':')
+ if type == '802-11-wireless':
+ print("Deleting connection", name)
+ cmd = "nmcli c delete {}".format(uuid)
+ print_cmd(cmd)
+ sp.call(cmd, shell=True)
+ print()
+
+
+def device_rescan():
+ print_head("Calling a rescan")
+ cmd = "nmcli d wifi rescan"
+ print_cmd(cmd)
+ sp.call(cmd, shell=True)
+ print()
+
+
+def list_aps(args):
+ print_head("List APs")
+ count = 0
+ cmd = "nmcli -t -f SSID,CHAN,FREQ,SIGNAL d wifi list ifname {}".format(
+ args.device)
+ print_cmd(cmd)
+ output = sp.check_output(cmd, shell=True)
+ for line in output.decode(sys.stdout.encoding).splitlines():
+ # lp bug #1723372 - extra line in output on zesty
+ if line.strip() == args.device:
+ continue
+ ssid, channel, frequency, signal = line.strip().split(':')
+ print("SSID: {} Chan: {} Freq: {} Signal: {}".format(
+ ssid, channel, frequency, signal))
+ if hasattr(args, 'essid'):
+ if ssid == args.essid:
+ count += 1
+ else:
+ count += 1
+ print()
+ return count
+
+
+def open_connection(args):
+ print_head("Connection attempt")
+ cmd = "nmcli d wifi connect {} ifname {} name TEST_CON".format(
+ args.essid, args.device)
+ print_cmd(cmd)
+ sp.call(cmd, shell=True)
+ cmd = "nmcli -m tabular -t -f GENERAL.STATE d show {}".format(args.device)
+ print_cmd(cmd)
+ output = sp.check_output(cmd, shell=True)
+ state = output.decode(sys.stdout.encoding).strip()
+ print(state)
+ rc = 1
+ if state.startswith('100'):
+ rc = 0
+ print()
+ return rc
+
+
+def secured_connection(args):
+ print_head("Connection attempt")
+ cmd = "nmcli d wifi connect {} password {} ifname {} name TEST_CON".format(
+ args.essid, args.psk, args.device)
+ print_cmd(cmd)
+ sp.call(cmd, shell=True)
+ cmd = "nmcli -m tabular -t -f GENERAL.STATE d show {}".format(args.device)
+ print_cmd(cmd)
+ output = sp.check_output(cmd, shell=True)
+ state = output.decode(sys.stdout.encoding).strip()
+ print(state)
+ rc = 1
+ if state.startswith('100'):
+ rc = 0
+ print()
+ return rc
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(
+ description='WiFi connection test using mmcli')
+
+ subparsers = parser.add_subparsers(dest='test_type')
+ subparsers.required = True
+
+ parser_scan = subparsers.add_parser(
+ 'scan', help='Test can scan for networks only')
+ parser_scan.add_argument(
+ 'device', type=str, help='Device name e.g. wlan0')
+
+ parser_open = subparsers.add_parser(
+ 'open', help='Test connection to an open access point')
+ parser_open.add_argument(
+ 'device', type=str, help='Device name e.g. wlan0')
+ parser_open.add_argument('essid', type=str, help='ESSID')
+ parser_open.set_defaults(func=open_connection)
+
+ parser_secured = subparsers.add_parser(
+ 'secured', help='Test connection to a secured access point')
+ parser_secured.add_argument(
+ 'device', type=str, help='Device name e.g. wlan0')
+ parser_secured.add_argument('essid', type=str, help='ESSID')
+ parser_secured.add_argument('psk', type=str, help='Pre-Shared Key')
+ parser_secured.set_defaults(func=secured_connection)
+ args = parser.parse_args()
+
+ cleanup_nm_connections()
+ device_rescan()
+ count = list_aps(args)
+
+ if args.test_type == 'scan':
+ if count == 0:
+ print("Failed to find any APs")
+ sys.exit(1)
+ else:
+ print("Found {} access points".format(count))
+ sys.exit(0)
+
+ if args.func:
+ try:
+ sys.exit(args.func(args))
+ finally:
+ cleanup_nm_connections()