def IsOnAndroidWap():
  """Checks if Android WiFi or Bluetooth tethering is connected.

  Returns:
    Boolean. True if Android tethering is connected, False otherwise.
  """
  # ifconfig output looks a little bit different on Darwin vs Linux.
  #
  # Darwin:
  # inet 169.254.135.20 netmask 0xffff0000 broadcast 169.254.255.255
  # Linux:
  # inet addr:172.26.113.45  Bcast:172.26.115.255  Mask:255.255.252.0
  android_wap_match_regex = re.compile(
      r'inet[\w\s]*[\s:]+192\.168\.(42|43|44)\.\d{1,3}\s+'
      r'.*(?:netmask\s+0xffffff00\s+|Mask:255\.255\.255\.0)')

  ifaces = GetInterfaceNames(INTERFACE_ANDROID_WAP)

  for wifi_iface in ifaces:
    # Android tethering uses very specific subnets*, as well as dnsmasq which
    # reveals itself via the TXT VERSION.BIND record.
    # * 192.168.42.0/24 for wired, 192.168.43.0/24 for WiFi, and
    #   192.168.44.0/24 for Bluetooth.
    try:
      return_code, stdout, stderr = flight_common.Exec([IFCONFIG, wifi_iface])
    except __HOLE__:
      return_code = None

    if return_code != 0 or stderr:  # interface was likely not found.
      continue

    android_wap_match = android_wap_match_regex.search(stdout)

    # Look for an interface on 192.168.4[2-4].0/24.
    if android_wap_match is not None:
      # If the default gateway is not through a likely Android WAN interface,
      # tethering may be active but is not likely to be used.
      default_gateway = GetDefaultGateway()
      logging.debug('Default gateway: %s', str(default_gateway))
      default_gateway_prefix = '192.168.%s.' % android_wap_match.group(1)
      if not default_gateway.startswith(default_gateway_prefix):
        return False

      # IP, netmask, gateway look like Android WAP, so check dnsmasq.

      # Request needs to be explicitly top level, as Linux uses
      # ndots:2 which would turn VERSION.BIND (without trailing dot) into
      # VERSION.BIND.foo.example.com in some cases.
      cmd = [HOST, '-W', '5', '-c', 'CHAOS', '-t', 'txt', 'VERSION.BIND.',
             default_gateway]
      try:
        return_code, stdout, unused_err = flight_common.Exec(cmd)
      except OSError:
        return_code = None
      if return_code != 0:
        continue
      dnsmasq_match = re.search(
          r'VERSION\.BIND descriptive text "dnsmasq-.*"', stdout)
      if dnsmasq_match is not None:
        # IP, netmask and dnsmasq all match Android WAP tethering.
        return True

  return False
dataset/ETHPy150Open google/simian/src/simian/mac/client/network_detect.py/IsOnAndroidWap