-2

My OS sistem is Windows 10.

I use android x86 in my emulator.

I used virtualbox as emulator machine and the network of virtualbox set to:

-Bridge Adapter (connection type)

-Intel(R) Dual Band Wireless-AC 3168 (Name)

-Intel PRO/1000 MT Desktop (8254OEM) -> (adapter type)

-Allow All (Promiscuous mode)

I set that because of that set is working to make my emulator connect to internet.

i use python to run a script that use the emulator.

the problem is when i use different network, the IP of the emulator is change..

so some part of my code is not working when i change the network that i use, the code is:

from subprocess import Popen, PIPE
import os

ip_emulator ='192.168.100.15' #the ip got from "ALT + F1" and type ipconfig on emulator
fileadb = 'adb.exe'
pathsys32 = r'C:\Users\admin\AppData\Local\Android\Sdk\platform-tools'
adb = os.path.join(pathsys32, fileadb)
cek = Popen([adb, 'connect', ip_emulator], stdout=PIPE, stderr=PIPE)

because of the ip change in emulator, caused by change my network that i used (eg, change from use network at home to office network or use mobile modem).

i should make a code to detect the emulator ip to make sure my adb is connected to emulator.

how to do that? to cek it outside the emulator by python, without get in to emulator (eg. press ALT + F1, and type ifconfig).

I already try use: "arp -a" but sometimes the ip show sometimes not.

already try "netstat -a" no emulator ip show there

the code to detect via arp:

from subprocess import Popen, PIPE
import os
import re

arpfile = 'ARP.EXE'
fileadb = 'adb.exe'
pathsys32 = r'C:\Windows\System32'
arp = os.path.join(pathsys32, arpfile)
adb = os.path.join(pathsys32, fileadb)

def detectiparp():
    cek = Popen([arp, '-a'], stdout=PIPE, stderr=PIPE)
    out, err = cek.communicate()
    outdata = (out.decode('utf-8')).split('\n')
    for i in outdata:
        mac = '08-00-27-ce-9e-8c'
        rexmac = re.search(mac, i)
        if rexmac:
            ipre = '([0-9]{0,}\.[0-9]{0,}\.[0-9]{0,}\.[0-9]{0,})\s.+?08-00-27-ce-9e-8c'
            iprex = re.search(ipre, i)
            ip_emu = iprex.group(1)
            return ip_emu
        else:
            return False

ip_emulator = detectiparp()

cek = Popen([adb, 'connect', ip_emulator], stdout=PIPE, stderr=PIPE)

I already read:

How to get the Android Emulator's IP address?

How to connect to AVD

How to get the IP address of an emulator to communicate to the local server

and i think all of the answer is "cek your emulator IP from inside the emulator" or "use the default set of IP (10.0.2.2)"

so, i explaining something, IP 10.0.2.2 only happens if the connection type that i use in virtualbox is NAT type, but with that setting, my emulator cannot connect to internet.

Already try use:

1st network set as NAT and 2nd Network set as Bridge, but the IP change to C class (192.169.x.x), and the emulator's ip still change if i use different network.

if i know how to detect my emulator IP (from host/ laptop / outside my emulator), i can make the code, or there is python module that i can use to detect it?

Wahyu Bram
  • 413
  • 1
  • 7
  • 13

1 Answers1

-1

ok, two days and no response...

and all question that have identical question, still no answer....

I already found the answer, so i share it here...anyone helped with my answer please give me rating...

tools to detect the emulator from outside the emulator is using 'arp -a', but before do it we must ping to the emulator first to make command 'arp -a' give the result.

so i make a script to automatically do it every time i turn on my script.

my emulator setting already explain before, what i'm not explaining is i turn on my emulator in headless mode....

this is my script:

from __future__ import print_function
import os
import time
import re
from subprocess import Popen, PIPE
from io import StringIO
from contextlib import redirect_stdout
import ipaddress as ipaddr
import socket

pathvbox = r'C:\Program Files\Oracle\VirtualBox'
pathadb = r'C:\Users\admin\AppData\Local\Android\Sdk\platform-tools'
fileadb = 'adb.exe'
filevboxmanage = 'VBoxManage.exe'
adb = os.path.join(pathadb, fileadb)
vbmanage = os.path.join(pathvbox, filevboxmanage)

arpfile = 'ARP.EXE'
pathsys32 = r'C:\Windows\System32'
arp = os.path.join(pathsys32, arpfile)

pathpy3 = r'C:\Users\admin\AppData\Local\Programs\Python\Python37'
py3file = 'python3.exe'
python3 = os.path.join(pathpy3, py3file)

def startemulator():
#function to run emulator in headless (Android x86)
    cek = Popen([vbmanage, 'startvm', 'andro', '--type', 'headless'], stdout=PIPE, stderr=PIPE)
    time.sleep(5)

def killemulator():
    #function to shutdown emulator in savestate condition
    sstate = Popen([vbmanage, 'controlvm', 'andro', 'savestate'], stdout=PIPE, stderr=PIPE)
    shutdown = Popen([vbmanage, 'controlvm', 'andro', 'savestate', 'shutdown', '/s', '/t', '10'], stdout=PIPE, stderr=PIPE)
    out, err = shutdown.communicate()

def ping(str):
    p1 = Popen(['ping', '-c', '3', str], stdout=PIPE, stderr=PIPE)
    p1.communicate()

def get_own_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    return s.getsockname()[0]

def ping_all_ip():
    ipnow = get_own_ip()
    inet = ipaddr.ip_interface(ipnow + '/24')
    allip = inet.network
    f = StringIO()
    with redirect_stdout(f):
        for ip in allip:
            print(ip)
        all_ip = f.getvalue().split('\n')

    for ip in all_ip:
        ping(ip)

def detectiparp():
    cek = Popen([arp, '-a'], stdout=PIPE, stderr=PIPE)
    out, err = cek.communicate()
    outdata = (out.decode('utf-8')).split('\n')
    for i in outdata:
        mac = '(08-00-27-ec-9c-8e)' #put your mac address here
        rexmac = re.search(mac, i)
        if rexmac:
            ipre = '([0-9]{0,}\.[0-9]{0,}\.[0-9]{0,}\.[0-9]{0,})\s.+?' + mac
            iprex = re.search(ipre, i)
            iponline = iprex.group(1)
        else:
            pass

    try:
        if iponline:
            return iponline
        else:
            return False
    except:
        return False

def detectip():
    while True:
        ping_all_ip()
        try:
            ipis = detectiparp()
            if ipis:
                break
        except:
            continue
    return ipis

def turnonADB():
    ip_emu = detectip()
    while True:
        cek = Popen([adb, 'connect', ip_emu], stdout=PIPE, stderr=PIPE)
        out, err = cek.communicate()
        outstring = out.decode('utf-8')
        try:
            failed = re.search('failed', outstring)
            if not failed:
                break
        except:
            continue

if __name__ == '__main__':
    startemulator()
    turnonADB()

That's all...

what i do is....:

just detect my ip...

then transform it to cidr /24,

then extract it again,

ping all ip that extracted

after pinging all ip in my network, turn on 'arp -a' command.

because of emulator needs some time to 'activate / turn on / live', so there is chance when you ping the emulator ip, it is not activate / not response, so this script automatically reprocess the ping and cek it in arp -a.

if 'arp -a' still not shown emulator mac address, means, the emulator is in process to turn on.

cheers..hope helping for anybody needs the answer.

admin may close this question....thanks....

Wahyu Bram
  • 413
  • 1
  • 7
  • 13