diff options
| author | Jonathan Cave <jonathan.cave@canonical.com> | 2019-02-20 11:05:41 +0000 |
|---|---|---|
| committer | Jonathan Cave <jonathan.cave@canonical.com> | 2019-02-20 14:24:36 +0000 |
| commit | e5ae9128ec358fd417433ba181faf522a55bdf16 (patch) | |
| tree | ef94cf53f01b935441cc984b25fbeefc4bab9977 /bin | |
| parent | 36f0620a97cc56a069fb2773bd559f5ce990981c (diff) | |
wireless: sleep on failure to request rescan
In wifi_nmcli_test on recent nmcli versions a rescan is requested to ensure an up-to-date list of APs is available. However this request can fail if NM has already started a scan, but we immediately attempt the connection. This should let the background scan complete.
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/wifi_nmcli_test.py (renamed from bin/wifi_nmcli_test) | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/bin/wifi_nmcli_test b/bin/wifi_nmcli_test.py index b89a525..45b8624 100755 --- a/bin/wifi_nmcli_test +++ b/bin/wifi_nmcli_test.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Copyright 2017-2018 Canonical Ltd. +# Copyright 2017-2019 Canonical Ltd. # All rights reserved. # # Written by: @@ -13,6 +13,7 @@ import argparse import functools import subprocess as sp import sys +import time from distutils.version import LooseVersion @@ -61,7 +62,13 @@ def device_rescan(): print_head("Calling a rescan") cmd = "nmcli d wifi rescan" print_cmd(cmd) - sp.call(cmd, shell=True) + retcode = sp.call(cmd, shell=True) + if retcode != 0: + # Most often the rescan request fails because NM has itself started + # a scan in recent past, we should let these operations complete before + # attempting a connection + print('Scan request failed, allow other operations to complete (15s)') + time.sleep(15) print() @@ -73,7 +80,8 @@ def list_aps(args): cmd = "nmcli -t -f {} d wifi list iface {}".format(fields, args.device) else: fields = "SSID,CHAN,FREQ,SIGNAL" - cmd = "nmcli -t -f {} d wifi list ifname {}".format(fields, args.device) + cmd = "nmcli -t -f {} d wifi list ifname {}".format( + fields, args.device) print_cmd(cmd) output = sp.check_output(cmd, shell=True) for line in output.decode(sys.stdout.encoding).splitlines(): @@ -108,10 +116,11 @@ def open_connection(args): print_cmd(cmd) sp.call(cmd, shell=True) if legacy_nmcli(): - cmd_part = "nmcli -m tabular -t -f GENERAL d list | " - cmd = cmd_part + "grep {} | awk -F: '{{print $15}}'".format(args.device) + cmd = ("nmcli -m tabular -t -f GENERAL d list | grep {} | " + "awk -F: '{{print $15}}'".format(args.device)) else: - cmd = "nmcli -m tabular -t -f GENERAL.STATE d show {}".format(args.device) + 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() @@ -126,18 +135,19 @@ def open_connection(args): def secured_connection(args): print_head("Connection attempt") if legacy_nmcli(): - cmd = "nmcli d wifi connect {} password {} iface {} name TEST_CON".format( - args.essid, args.psk, args.device) + cmd = ("nmcli d wifi connect {} password {} iface {} name " + "TEST_CON".format(args.essid, args.psk, args.device)) else: - cmd = "nmcli d wifi connect {} password {} ifname {} name TEST_CON".format( - args.essid, args.psk, args.device) + 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) if legacy_nmcli(): - cmd_part = "nmcli -m tabular -t -f GENERAL d list | " - cmd = cmd_part + "grep {} | awk -F: '{{print $15}}'".format(args.device) + cmd = ("nmcli -m tabular -t -f GENERAL d list | " + "grep {} | awk -F: '{{print $15}}'".format(args.device)) else: - cmd = "nmcli -m tabular -t -f GENERAL.STATE d show {}".format(args.device) + 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() |
