1#!/usr/bin/env python3

2# Copyright 2015 Canonical Ltd.

3# All rights reserved.

4#

5# Written by:

6# Authors: Gavin Lin <gavin.lin@canonical.com>

7

8"""

9 This script will run wireless network test automatically.

10

11 To see how to use, please run "./wifi_client_test.py --help"

12"""

13 import argparse

14 import os

15 import subprocess

16 import sys

17 import time

18

19

20# Configuration file path

21 CFG_PATH = "/etc/network/interfaces.d/plainbox_wifi_client_test"

22

23# Use ping to check connection? (yes/no)

24 PING_TEST = "no"

25

26# Use DHCP to get ip address? (yes/no) (This may be required for ping test)

27 DHCP_IP = "no"

28

29# Read arguments

30 parser = argparse.ArgumentParser(

31 description=("This script will test wireless network in client mode.")

32)

33 parser.add_argument(

34 "-i",

35 "--interface",

36 type=str,

37 help=("The interface which will be tested, default is wlan0"),

38 default="wlan0",

39)

40 parser.add_argument(

41 "-s",

42 "--ssid",

43 type=str,

44 help=("SSID of target network, this is required argument"),

45 required=True,

46)

47 parser.add_argument(

48 "-k",

49 "--psk",

50 type=str,

51 help=(

52 "Pre-shared key of target network, this is optional argument,"

53 " only for PSK protected network"

54 ),

55)

56 if PING_TEST == "yes":

57 parser.add_argument(

58 "-p",

59 "--ping",

60 type=str,

61 help=(

62 "Server in target network which respond to ping,"

63 " default is ubuntu.com"

64 ),

65 default="ubuntu.com",

66 )

67 args = parser.parse_args()

68

69# Create wireless network configuration file

70 wifi_cfg_file = open(CFG_PATH, "w", encoding="utf-8")

71# DHCP or static ip

72 if DHCP_IP == "yes":

73 wifi_cfg_file.write("iface " + args.interface + " inet dhcp\n")

74 else:

75 wifi_cfg_file.write("iface " + args.interface + " inet static\n")

76 wifi_cfg_file.write(" address 192.168.1.1\n")

77# SSID

78 wifi_cfg_file.write(" wpa-ssid " + '"' + args.ssid + '"\n')

79# Opened network or PSK protected network

80 if not args.psk:

81 wifi_cfg_file.write(" wpa-key-mgmt NONE\n")

82 else:

83 wifi_cfg_file.write(" wpa-psk " + '"' + args.psk + '"\n')

84 wifi_cfg_file.close()

85

86# Bring up the interface

87 subprocess.call(["ip", "link", "set", args.interface, "up"])

88 time.sleep(15)

89 subprocess.call(["ifdown", args.interface])

90 time.sleep(15)

91 subprocess.call(["ifup", args.interface])

92 time.sleep(15)

93

94# Check connection

95 if PING_TEST == "yes":

96 test_result = subprocess.call(

97 ["ping", "-c", "5", "-I", args.interface, args.ping]

98 )

99 if not test_result:

100 print("Connection test passed")

101 exit_code = 0

102 else:

103 print("Connection test failed")

104 exit_code = 1

105 else:

106 test_result = subprocess.check_output(

107 ["iw", args.interface, "link"]

108 ).decode()

109 if test_result.find("SSID") != -1:

110 print("Connection test passed")

111 exit_code = 0

112 else:

113 print("Connection test failed")

114 exit_code = 1

115

116# Bring down the interface and remove configuration file after test

117 subprocess.call(["ifdown", args.interface])

118 time.sleep(15)

119 os.remove(CFG_PATH)

120 sys.exit(exit_code)