From ad0cc790943c1e5d4d875b0295f597c757eac8df Mon Sep 17 00:00:00 2001 From: Jonathan Cave Date: Fri, 7 Feb 2020 16:46:19 +0000 Subject: wifi_master_mode: rename script --- bin/wifi_master_mode | 91 ------------------------------------------------- bin/wifi_master_mode.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 91 deletions(-) delete mode 100755 bin/wifi_master_mode create mode 100755 bin/wifi_master_mode.py (limited to 'bin') diff --git a/bin/wifi_master_mode b/bin/wifi_master_mode deleted file mode 100755 index 5953ea34..00000000 --- a/bin/wifi_master_mode +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/env python3 -# Copyright 2015 Canonical Ltd. -# All rights reserved. -# -# Written by: -# Jonathan Cave -# Po-Hsu Lin - -import logging -import os -import sys -import subprocess -import tempfile - -from guacamole import Command - - -class WifiMasterMode(Command): - - """Make system to act as an 802.11 Wi-Fi Access Point.""" - - def register_arguments(self, parser): - parser.add_argument('--protocol', default='g', - choices=['a', 'b', 'g', 'ad'], - help="802.11 protocol, possible value: a/b/g/ad") - parser.add_argument('--auto', action='store_true', - help="Run in the automated mode") - - def invoked(self, ctx): - data_dir = "" - try: - data_dir = os.environ['PLAINBOX_PROVIDER_DATA'] - except KeyError: - logging.error("PLAINBOX_PROVIDER_DATA variable not set") - return 1 - logging.info("Provider data dir: {}".format(data_dir)) - - wifi_dev = "wlan0" - try: - wifi_dev = os.environ['WIFI_AP_DEV'] - except KeyError: - logging.info("WIFI_AP_DEV variable not set, defaulting to wlan0") - logging.info("Wi-Fi adapter: {}".format(wifi_dev)) - - conf_in = os.path.join(data_dir, 'hostapd.conf.in') - if not os.path.isfile(conf_in): - logging.error("Couldn't find {}".format(conf_in)) - return 1 - - with tempfile.NamedTemporaryFile(mode='w+t') as conf_file_out: - with open(conf_in, "r") as conf_file_in: - data_in = conf_file_in.read() - data_out = data_in.replace("$PROTOCOL", ctx.args.protocol) - data_out = data_out.replace("$WIFI-DEV-NAME", wifi_dev) - conf_file_out.write(data_out) - conf_file_out.flush() - - if ctx.args.auto: - child = subprocess.Popen(['hostapd', '-d', conf_file_out.name], - stdout=subprocess.PIPE, - universal_newlines=True) - log = '' - while child.poll() is None: - output = child.stdout.readline() - log += output - if 'AP-ENABLED' in output: - logging.info(output) - logging.info("AP successfully established.") - child.terminate() - if child.poll() is not 0: - output = child.stdout.read() - logging.error(log + output) - logging.error('AP failed to start') - return child.poll() - else: - # Print and flush this or it will get buffered during test - print("Hit any key to start the Access Point, a second key " + - "press will stop the Access Point:") - sys.stdout.flush() - input() - - try: - child = subprocess.Popen(['hostapd', conf_file_out.name]) - - # kill the process on input - input() - finally: - child.terminate() - -if __name__ == "__main__": - WifiMasterMode().main() diff --git a/bin/wifi_master_mode.py b/bin/wifi_master_mode.py new file mode 100755 index 00000000..5953ea34 --- /dev/null +++ b/bin/wifi_master_mode.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +# Copyright 2015 Canonical Ltd. +# All rights reserved. +# +# Written by: +# Jonathan Cave +# Po-Hsu Lin + +import logging +import os +import sys +import subprocess +import tempfile + +from guacamole import Command + + +class WifiMasterMode(Command): + + """Make system to act as an 802.11 Wi-Fi Access Point.""" + + def register_arguments(self, parser): + parser.add_argument('--protocol', default='g', + choices=['a', 'b', 'g', 'ad'], + help="802.11 protocol, possible value: a/b/g/ad") + parser.add_argument('--auto', action='store_true', + help="Run in the automated mode") + + def invoked(self, ctx): + data_dir = "" + try: + data_dir = os.environ['PLAINBOX_PROVIDER_DATA'] + except KeyError: + logging.error("PLAINBOX_PROVIDER_DATA variable not set") + return 1 + logging.info("Provider data dir: {}".format(data_dir)) + + wifi_dev = "wlan0" + try: + wifi_dev = os.environ['WIFI_AP_DEV'] + except KeyError: + logging.info("WIFI_AP_DEV variable not set, defaulting to wlan0") + logging.info("Wi-Fi adapter: {}".format(wifi_dev)) + + conf_in = os.path.join(data_dir, 'hostapd.conf.in') + if not os.path.isfile(conf_in): + logging.error("Couldn't find {}".format(conf_in)) + return 1 + + with tempfile.NamedTemporaryFile(mode='w+t') as conf_file_out: + with open(conf_in, "r") as conf_file_in: + data_in = conf_file_in.read() + data_out = data_in.replace("$PROTOCOL", ctx.args.protocol) + data_out = data_out.replace("$WIFI-DEV-NAME", wifi_dev) + conf_file_out.write(data_out) + conf_file_out.flush() + + if ctx.args.auto: + child = subprocess.Popen(['hostapd', '-d', conf_file_out.name], + stdout=subprocess.PIPE, + universal_newlines=True) + log = '' + while child.poll() is None: + output = child.stdout.readline() + log += output + if 'AP-ENABLED' in output: + logging.info(output) + logging.info("AP successfully established.") + child.terminate() + if child.poll() is not 0: + output = child.stdout.read() + logging.error(log + output) + logging.error('AP failed to start') + return child.poll() + else: + # Print and flush this or it will get buffered during test + print("Hit any key to start the Access Point, a second key " + + "press will stop the Access Point:") + sys.stdout.flush() + input() + + try: + child = subprocess.Popen(['hostapd', conf_file_out.name]) + + # kill the process on input + input() + finally: + child.terminate() + +if __name__ == "__main__": + WifiMasterMode().main() -- cgit v1.2.3 From 4ec088307e35a38770c0441d565fc627fe463e44 Mon Sep 17 00:00:00 2001 From: Jonathan Cave Date: Fri, 7 Feb 2020 16:51:52 +0000 Subject: wifi_master_mode: remove use of guacamole module --- bin/wifi_master_mode.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'bin') diff --git a/bin/wifi_master_mode.py b/bin/wifi_master_mode.py index 5953ea34..3888caa6 100755 --- a/bin/wifi_master_mode.py +++ b/bin/wifi_master_mode.py @@ -1,32 +1,32 @@ #!/usr/bin/env python3 -# Copyright 2015 Canonical Ltd. +# Copyright 2015-2020 Canonical Ltd. # All rights reserved. # # Written by: # Jonathan Cave # Po-Hsu Lin +import argparse import logging import os import sys import subprocess import tempfile -from guacamole import Command - -class WifiMasterMode(Command): +class WifiMasterMode(): """Make system to act as an 802.11 Wi-Fi Access Point.""" - def register_arguments(self, parser): + def main(self): + parser = argparse.ArgumentParser() parser.add_argument('--protocol', default='g', choices=['a', 'b', 'g', 'ad'], help="802.11 protocol, possible value: a/b/g/ad") parser.add_argument('--auto', action='store_true', help="Run in the automated mode") + args = parser.parse_args() - def invoked(self, ctx): data_dir = "" try: data_dir = os.environ['PLAINBOX_PROVIDER_DATA'] @@ -50,12 +50,12 @@ class WifiMasterMode(Command): with tempfile.NamedTemporaryFile(mode='w+t') as conf_file_out: with open(conf_in, "r") as conf_file_in: data_in = conf_file_in.read() - data_out = data_in.replace("$PROTOCOL", ctx.args.protocol) + data_out = data_in.replace("$PROTOCOL", args.protocol) data_out = data_out.replace("$WIFI-DEV-NAME", wifi_dev) conf_file_out.write(data_out) conf_file_out.flush() - if ctx.args.auto: + if args.auto: child = subprocess.Popen(['hostapd', '-d', conf_file_out.name], stdout=subprocess.PIPE, universal_newlines=True) @@ -87,5 +87,6 @@ class WifiMasterMode(Command): finally: child.terminate() + if __name__ == "__main__": WifiMasterMode().main() -- cgit v1.2.3