def get_gateway_address(self): """ Perform several set of commands to obtain the gateway address * `adb getprop dhcp.wlan0.gateway` * `adb shell netcfg | grep wlan0` Returns: None if no gateway address has been found, otherwise return the gateway address """ ip2int = lambda ip: reduce(lambda a, b: (a << 8) + b, map(int, ip.split('.')), 0) int2ip = lambda n: '.'.join( [str(n >> (i << 3) & 0xFF) for i in range(0, 4)[::-1]]) try: res = self.shell('getprop dhcp.wlan0.gateway') except AdbShellError: res = '' matcher = IP_PATTERN.search(res) if matcher: return matcher.group(0) ip = self.get_ip_address() if not ip: return None mask_len = self._get_subnet_mask_len() gateway = (ip2int(ip) & (((1 << mask_len) - 1) << (32 - mask_len))) + 1 return int2ip(gateway)
def get_ip_address(self): """ Perform several set of commands to obtain the IP address * `adb shell netcfg | grep wlan0` * `adb shell ifconfig` * `adb getprop dhcp.wlan0.ipaddress` Returns: None if no IP address has been found, otherwise return the IP address """ try: res = self.shell('netcfg') except AdbShellError: res = '' matcher = re.search(r'wlan0.* ((\d+\.){3}\d+)/\d+', res) if matcher: return matcher.group(1) else: try: res = self.shell('ifconfig') except AdbShellError: res = '' matcher = re.search(r'wlan0.*?inet addr:((\d+\.){3}\d+)', res, re.DOTALL) if matcher: return matcher.group(1) else: try: res = self.shell('getprop dhcp.wlan0.ipaddress') except AdbShellError: res = '' matcher = IP_PATTERN.search(res) if matcher: return matcher.group(0) return None
def get_ip_address_from_interface(interface): try: res = self.shell('netcfg') except AdbShellError: res = '' matcher = re.search(interface + r'.* ((\d+\.){3}\d+)/\d+', res) if matcher: return matcher.group(1) else: try: res = self.shell('ifconfig') except AdbShellError: res = '' matcher = re.search( interface + r'.*?inet addr:((\d+\.){3}\d+)', res, re.DOTALL) if matcher: return matcher.group(1) else: try: res = self.shell( 'getprop dhcp.{}.ipaddress'.format(interface)) except AdbShellError: res = '' matcher = IP_PATTERN.search(res) if matcher: return matcher.group(0) return None
def get_ip_address_from_interface(interface): """Get device ip from target network interface.""" # android >= 6.0: ip -f inet addr show {interface} try: res = self.shell('ip -f inet addr show {}'.format(interface)) except AdbShellError: res = '' matcher = re.search(r"inet (\d+\.){3}\d+", res) if matcher: return matcher.group().split(" ")[-1] # android >= 6.0 backup method: ifconfig try: res = self.shell('ifconfig') except AdbShellError: res = '' matcher = re.search(interface + r'.*?inet addr:((\d+\.){3}\d+)', res, re.DOTALL) if matcher: return matcher.group(1) # android <= 6.0: netcfg try: res = self.shell('netcfg') except AdbShellError: res = '' matcher = re.search(interface + r'.* ((\d+\.){3}\d+)/\d+', res) if matcher: return matcher.group(1) # android <= 6.0 backup method: getprop dhcp.{}.ipaddress try: res = self.shell('getprop dhcp.{}.ipaddress'.format(interface)) except AdbShellError: res = '' matcher = IP_PATTERN.search(res) if matcher: return matcher.group(0) # sorry, no more methods... return None