获取emulator Ip以连接到android emulator(virtualbox)

获取emulator Ip以连接到android emulator(virtualbox),ip,adb,emulation,Ip,Adb,Emulation,我的操作系统是Windows10 我在模拟器中使用安卓x86 我使用virtualbox作为仿真器机器,virtualbox的网络设置为: -网桥适配器连接类型 -IntelR双频无线AC 3168名称 -英特尔PRO/1000 MT桌面8254OEM->适配器类型 -允许所有混乱模式 我之所以设置它,是因为它正在使我的模拟器连接到互联网 我使用python来运行使用仿真器的脚本 问题是当我使用不同的网络时,仿真器的IP发生了变化 因此,当我更改我使用的网络时,我的部分代码不起作用,代码是: f

我的操作系统是Windows10

我在模拟器中使用安卓x86

我使用virtualbox作为仿真器机器,virtualbox的网络设置为:

-网桥适配器连接类型

-IntelR双频无线AC 3168名称

-英特尔PRO/1000 MT桌面8254OEM->适配器类型

-允许所有混乱模式

我之所以设置它,是因为它正在使我的模拟器连接到互联网

我使用python来运行使用仿真器的脚本

问题是当我使用不同的网络时,仿真器的IP发生了变化

因此,当我更改我使用的网络时,我的部分代码不起作用,代码是:

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)
由于仿真器中的ip变化,由我使用的网络变化引起,例如,从在家使用网络更改为办公网络或使用移动调制解调器

我应该编写一个代码来检测仿真器ip,以确保我的adb连接到仿真器

怎么做?要通过python在模拟器外部执行此操作,而不进入模拟器,例如,按ALT+F1,然后键入ifconfig

我已经尝试使用:arp-a 但有时ip秀有时不是

已经尝试netstat-a了吗 那里没有模拟器ip显示

要通过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)
我已经读过:

我认为所有的答案都是从模拟器内部cek你的模拟器IP或者使用默认的IP 10.0.2.2

因此,我要解释一下,只有当我在virtualbox中使用的连接类型是NAT类型时,才会出现IP 10.0.2.2,但在该设置下,我的模拟器无法连接到internet

已尝试使用:

第一个网络设置为NAT,第二个网络设置为网桥,但IP更改为C类192.169.x.x,如果使用不同的网络,模拟器的IP仍然会更改


如果我知道如何从主机/笔记本电脑/仿真器外部检测我的仿真器IP,我可以编写代码,或者我可以使用python模块来检测它?

好的,两天没有响应

所有的问题都有相同的问题,仍然没有答案

我已经找到了答案,所以我在这里分享它…任何有助于我的答案的人请给我评分

用于从仿真器外部检测仿真器的工具使用的是“arp-a”,但在执行此操作之前,我们必须首先ping仿真器,使命令“arp-a”给出结果

所以我制作了一个脚本,每次打开脚本时都会自动执行

我的模拟器设置之前已经解释过了,我没有解释的是我在无头模式下打开模拟器

这是我的剧本:

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()
就这些

我所做的是

只要检测我的ip

然后将其转换为cidr/24

然后再把它取出来

ping提取的所有ip

ping完网络中的所有ip后,打开“arp-a”命令

因为emulator需要一些时间来“激活/打开/激活”,所以当您ping emulator ip时,它不是激活/不响应,所以此脚本自动重新处理ping并在arp-a中cek它

如果“arp-a”仍然没有显示模拟器的mac地址,则表示模拟器正在打开

干杯。希望帮助任何需要答案的人

管理员可能会关闭此问题…谢谢