def get_target_ip(self): logging.info("Waiting for network to come up") try: self.wait_network_up() except NetworkError: logging.exception("Unable to reach LAVA server") raise # add by Bo, 2016.01.22 # use busybox commands pattern1 = "<(\d?\d?\d?\.\d?\d?\d?\.\d?\d?\d?\.\d?\d?\d?)>" # cmd = ("ifconfig `ip route get %s | sed 's/.*via\ //' | cut -d ' ' -f3` | grep 'inet addr' |" # "awk -F: '{split($2,a,\" \"); print \"<\" a[1] \">\"}'" % # self._client.context.config.lava_server_ip) cmd = ("busybox ifconfig `ip route get %s | busybox sed 's/.*via\ //' | busybox cut -d ' ' -f3` | grep " "'inet addr' | busybox awk -F: '{split($2,a,\" \"); print \"<\" a[1] \">\"}'" % self._client.context.config.lava_server_ip) self.run( cmd, [pattern1, pexpect.EOF, pexpect.TIMEOUT], timeout=300) if self.match_id != 0: msg = "Unable to determine target image IP address" logging.exception(msg) raise NetworkError(msg) ip = self.match.group(1) if ip == "127.0.0.1": msg = "Got localhost (127.0.0.1) as IP address" logging.exception(msg) raise NetworkError(msg) logging.debug("Target image IP is %s" % ip) return ip
def _get_default_nic_ip_by_ifconfig(self, nic_name): # Check network ip and setup adb connection try: self.wait_network_up() except KeyboardInterrupt: raise KeyboardInterrupt except: logging.warning(traceback.format_exc()) return None ip_pattern1 = "%s: ip (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) mask" % nic_name ip_pattern2 = "inet addr:(\d?\d?\d?\.\d?\d?\d?\.\d?\d?\d?\.\d?\d?\d?) +Bcast:" try: self.run("ifconfig %s" % nic_name, [ip_pattern1, ip_pattern2, pexpect.EOF], timeout=60) except Exception as e: raise NetworkError("ifconfig can not match ip pattern for %s:%s" % (nic_name, e)) # keep the match_id range in line with the number of patterns. if self.match_id in [0, 1]: match_group = self.match.groups() if len(match_group) > 0: return match_group[0] return None
def _get_default_nic_ip_by_ifconfig(self, nic_name): # Check network ip and setup adb connection try: self.wait_network_up() except KeyboardInterrupt: raise KeyboardInterrupt except: logging.warning(traceback.format_exc()) return None ip_pattern = "%s: ip (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) mask" % nic_name try: self.run( "ifconfig %s" % nic_name, [ip_pattern, pexpect.EOF], timeout=60) except Exception as e: raise NetworkError("ifconfig can not match ip pattern for %s:%s" % (nic_name, e)) if self.match_id == 0: match_group = self.match.groups() if len(match_group) > 0: return match_group[0] return None