1#!/usr/bin/env python3

2# Copyright 2017 Canonical Ltd.

3# Written by:

4# Maciej Kisielewski <maciej.kisielewski@canonical.com>

5

6"""This program talks to wifi-ap.setup-wizard and sets up predefined AP"""

7

8 from checkbox_support.interactive_cmd import InteractiveCommand

9 import select

10 import subprocess

11 import sys

12

13

14 class InteractiveCommandWithShell(InteractiveCommand):

15 # this exists only to everride how subprocess.Popen is called (the addition

16 # of shell=True. If InteractiveCommand gets this as default, or an option

17 # this class won't be needed

18 def start(self):

19 self._proc = subprocess.Popen(

20 self._args,

21 stdin=subprocess.PIPE,

22 stdout=subprocess.PIPE,

23 stderr=subprocess.STDOUT,

24 shell=True,

25 )

26 self._is_running = True

27 self._poller = select.poll()

28 self._poller.register(self._proc.stdout, select.POLLIN)

29

30

31 def main():

32 if len(sys.argv) < 3:

33 raise SystemExit(

34 "Usage: wifi_ap_wizard.py WLAN_INTERFACE SHARED_INTERFACE"

35 )

36 wlan_iface = sys.argv[1]

37 shared_iface = sys.argv[2]

38

39 with InteractiveCommandWithShell("wifi-ap.setup-wizard") as wizard:

40 steps = [

41 (

42 "Which SSID you want to use for the access point",

43 "Ubuntu_Wizard",

44 ),

45 ("Do you want to protect your network with a WPA2 password", "y"),

46 ("Please enter the WPA2 passphrase", "Test1234"),

47 ("Insert the Access Point IP address", "192.168.42.1"),

48 ("How many host do you want your DHCP pool to hold to", "100"),

49 ("Do you want to enable connection sharing?", "y"),

50 (

51 "Which network interface you want to use for connection sharing?", # noqa: E501

52 shared_iface,

53 ),

54 ("Do you want to enable the AP now?", "y"),

55 ]

56 iface_choice_prompt = "Which wireless interface"

57 wizard.wait_for_output()

58 first_prompt = wizard.read_all()

59

60 if iface_choice_prompt in first_prompt:

61 wizard.writeline(wlan_iface)

62 else:

63 # the prompt is already consumed, so let's write a response

64 wizard.writeline(steps[0][1])

65 # and proceed with next steps

66 steps = steps[1:]

67

68 for prompt, response in steps:

69 if wizard.wait_until_matched(prompt, 1) is None:

70 raise SystemExit('Did not get prompted ("{}")'.format(prompt))

71 wizard.writeline(response)

72

73

74 if __name__ == "__main__":

75 main()