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 test_ssh_run(self): """Test running an ssh command. Test that running an ssh command works. """ conn = connection.SshConnection(self.settings) result = conn.run('echo "Hello World"') self.assertTrue(result.stdout.find('Hello World') > -1)
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 test_ssh_unknown_host(self): """Test that permission denied works. Connect to a remote host using an invalid username and see if we are rejected. """ with self.assertRaises(error.SshUnknownHost): bad_settings = settings.SshSettings('BADHOSTNAME', SSH_USER) conn = connection.SshConnection(bad_settings) result = conn.run('echo "Hello World"')
def test_ssh_permission_denied(self): """Test that permission denied works. Connect to a remote host using an invalid username and see if we are rejected. """ with self.assertRaises(error.SshPermissionDeniedError): bad_settings = settings.SshSettings(SSH_HOST, SSH_USER + 'BAD') conn = connection.SshConnection(bad_settings) result = conn.run('echo "Hello World"')
def test_ssh_env(self): """Test that special envirment variables get sent over ssh. Test that given a dictonary of enviroment variables they will be sent to the remote host. """ conn = connection.SshConnection(self.settings) result = conn.run('printenv', env={'MYSPECIALVAR': 20}) self.assertTrue(result.stdout.find('MYSPECIALVAR=20'))
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 start_ssh(self): """Starts an ssh session to the iperf client.""" if not self._ssh_session: if self._use_paramiko: self._ssh_session = create_ssh_connection( ip_address=self._ssh_settings.hostname, ssh_username=self._ssh_settings.username, ssh_config=self._ssh_settings.ssh_config) else: self._ssh_session = connection.SshConnection( self._ssh_settings)
def __init__(self, ssh_settings): """ Args: ssh_settings: acts.controllers.utils_lib.ssh.SshSettings instance. """ self.ssh_settings = ssh_settings 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()
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 start_ssh(self): """Starts an ssh session to the iperf server.""" if not self._ssh_session: self._ssh_session = connection.SshConnection(self.ssh_settings)