def get_instances_with_configs(configs): """Create AndroidDevice instances from a list of json configs. Each config should have the required key-value pair "serial". Args: configs: A list of dicts each representing the configuration of one android device. Returns: A list of AndroidDevice objects. """ results = [] for c in configs: try: serial = c.pop("serial") except KeyError: raise AndroidDeviceError( "Required value 'serial' is missing in AndroidDevice config %s." % c) ssh_config = c.pop("ssh_config", None) ssh_connection = None if ssh_config is not None: ssh_settings = settings.from_config(ssh_config) ssh_connection = connection.SshConnection(ssh_settings) ad = AndroidDevice(serial, ssh_connection=ssh_connection) ad.load_config(c) results.append(ad) return results
def check_wifi_status(pri_ad, network, ssh_config=None): """Function to check existence of wifi connection. Args: pri_ad: An android device. network: network ssid. ssh_config: ssh config for iperf client. """ time.sleep(5) proc = job.run("pgrep -f 'iperf3 -c'") pid_list = proc.stdout.split() while True: iperf_proc = job.run(["pgrep", "-f", "iperf3"]) process_list = iperf_proc.stdout.split() if not wifi_connection_check(pri_ad, network["SSID"]): pri_ad.adb.shell("killall iperf3") if ssh_config: time.sleep(5) ssh_settings = settings.from_config(ssh_config) ssh_session = connection.SshConnection(ssh_settings) result = ssh_session.run("pgrep iperf3") res = result.stdout.split("\n") for pid in res: try: ssh_session.run("kill -9 %s" % pid) except Exception as e: logging.warning("No such process: %s" % e) for pid in pid_list[:-1]: job.run(["kill", " -9", " %s" % pid], ignore_status=True) else: job.run(["killall", " iperf3"], ignore_status=True) break elif pid_list[0] not in process_list: break
def __init__(self, config): """Initialize AP.""" self.ssh_settings = settings.from_config(config["ssh_config"]) self.ssh = connection.SshConnection(self.ssh_settings) self.log = logger.create_logger(lambda msg: "[OpenWrtAP|%s] %s" % (self.ssh_settings.hostname, msg)) self.wireless_setting = None self.network_setting = network_settings.NetworkSettings( self.ssh, config["ssh_config"]["host"], self.log)
def __init__(self, config): if type(config) is dict and "ssh_config" in config: self.ssh_settings = settings.from_config(config["ssh_config"]) self.ssh_session = connection.SshConnection(self.ssh_settings) self.client_type = "remote" else: self.client_type = config["type"] self.iperf_process = None self.log_files = []
def __init__(self, config, log_path): self.server_type = "remote" self.ssh_settings = settings.from_config(config["ssh_config"]) self.ssh_session = connection.SshConnection(self.ssh_settings) self.port = config["port"] self.log_path = os.path.join(log_path, "iPerf{}".format(self.port)) utils.create_dir(self.log_path) self.iperf_str = "iperf3 -s -J -p {}".format(self.port) self.log_files = [] self.started = False
def __init__(self, ssh_config, use_paramiko=False, test_interface=None): self._ssh_settings = settings.from_config(ssh_config) # use_paramiko may be passed in as a string (from JSON), so this line # guarantees it is a converted to a bool. self._use_paramiko = str(use_paramiko).lower() == 'true' self._ssh_session = None self.start_ssh() self.hostname = self._ssh_settings.hostname self.test_interface = test_interface
def create(configs): """Creates ap controllers from a json config. Creates an ap controller from either a list, or a single element. The element can either be just the hostname or a dictionary containing the hostname and username of the ap to connect to over ssh. Args: The json configs that represent this controller. Returns: A new AccessPoint. """ return [ AccessPoint(settings.from_config(c['ssh_config'])) for c in configs ]
def __init__(self, configs): """ Args: configs: configs for the access point from config file. """ self.ssh_settings = settings.from_config(configs['ssh_config']) self.log = logger.create_logger(lambda msg: '[Access Point|%s] %s' % (self.ssh_settings.hostname, msg)) self.device_pdu_config = configs.get('PduDevice', None) self.identifier = self.ssh_settings.hostname if 'ap_subnet' in configs: self._AP_2G_SUBNET_STR = configs['ap_subnet']['2g'] self._AP_5G_SUBNET_STR = configs['ap_subnet']['5g'] else: self._AP_2G_SUBNET_STR = _AP_2GHZ_SUBNET_STR_DEFAULT self._AP_5G_SUBNET_STR = _AP_5GHZ_SUBNET_STR_DEFAULT self._AP_2G_SUBNET = dhcp_config.Subnet( ipaddress.ip_network(self._AP_2G_SUBNET_STR)) self._AP_5G_SUBNET = dhcp_config.Subnet( ipaddress.ip_network(self._AP_5G_SUBNET_STR)) self.ssh = connection.SshConnection(self.ssh_settings) # Singleton utilities for running various commands. self._ip_cmd = ip.LinuxIpCommand(self.ssh) self._route_cmd = route.LinuxRouteCommand(self.ssh) # A map from network interface name to _ApInstance objects representing # the hostapd instance running against the interface. self._aps = dict() self._dhcp = None self._dhcp_bss = dict() self.bridge = bridge_interface.BridgeInterface(self) self.interfaces = ap_get_interface.ApInterfaces(self) self.iwconfig = ap_iwconfig.ApIwconfig(self) # Get needed interface names and initialize the unneccessary ones. self.wan = self.interfaces.get_wan_interface() self.wlan = self.interfaces.get_wlan_interface() self.wlan_2g = self.wlan[0] self.wlan_5g = self.wlan[1] self.lan = self.interfaces.get_lan_interface() self._initial_ap() self.scapy_install_path = None self.setup_bridge = False
def start_fping(pri_ad, duration, fping_params): """Starts fping to ping for DUT's ip address. Steps: 1. Run fping command to check DUT's IP is alive or not. Args: pri_ad: An android device object. duration: Duration of fping in seconds. fping_params: List of parameters for fping to run. Returns: True if successful, False otherwise. """ counter = 0 fping_path = ''.join((pri_ad.log_path, "/Fping")) os.makedirs(fping_path, exist_ok=True) while os.path.isfile(fping_path + "/fping_%s.txt" % counter): counter += 1 out_file_name = "{}".format("fping_%s.txt" % counter) full_out_path = os.path.join(fping_path, out_file_name) cmd = "fping {} -D -c {}".format(get_phone_ip(pri_ad), duration) if fping_params["ssh_config"]: ssh_settings = settings.from_config(fping_params["ssh_config"]) ssh_session = connection.SshConnection(ssh_settings) try: with open(full_out_path, 'w') as outfile: job_result = ssh_session.run(cmd) outfile.write(job_result.stdout) outfile.write("\n") outfile.writelines(job_result.stderr) except Exception as err: pri_ad.log.error("Fping run has been failed. = {}".format(err)) return False else: cmd = cmd.split() with open(full_out_path, "w") as f: job.run(cmd) result = parse_fping_results(fping_params["fping_drop_tolerance"], full_out_path) return bool(result)
def __init__(self, configs): """Initialize objects. Args: configs: config for the packet capture. """ self.ssh_settings = settings.from_config(configs['ssh_config']) self.ssh = connection.SshConnection(self.ssh_settings) self.log = logger.create_logger(lambda msg: '[%s|%s] %s' % ( MOBLY_CONTROLLER_CONFIG_NAME, self.ssh_settings.hostname, msg)) self._create_interface(MON_2G, 'monitor') self._create_interface(MON_5G, 'monitor') self.managed_mode = True result = self.ssh.run('ifconfig -a', ignore_status=True) if result.stderr or SCAN_IFACE not in result.stdout: self.managed_mode = False if self.managed_mode: self._create_interface(SCAN_IFACE, 'managed') self.pcap_properties = dict() self._pcap_stop_lock = threading.Lock()
def __init__(self, ssh_config, port, test_interface=None, use_killall=False): super().__init__(port) self.ssh_settings = settings.from_config(ssh_config) self._ssh_session = None self.start_ssh() self._iperf_pid = None self._current_tag = None self.hostname = self.ssh_settings.hostname self._use_killall = str(use_killall).lower() == 'true' try: # A test interface can only be found if an ip address is specified. # A fully qualified hostname will return None for the # test_interface. self.test_interface = self._get_test_interface_based_on_ip( test_interface) except Exception: self.test_interface = None