首页 > 解决方案 > 使用“VirtualBox Host-Only”从批处理脚本中获取机器的 IP 地址

问题描述

我想使用批处理文件捕获我机器的 IP 地址。

我正在使用以下代码:

for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set IPAddress=%%a
echo IP-Adress is %IPAddress%

我的系统中也安装了 Virtual Box。因此,还为VB名称安装了以太网适配器Ethernet adapter VirtualBox Host-Only Network

现在,每当我使用 ping 我的机器时ComputerName,我都会收到来自VB Host-Only Network适配器的响应。

ping %ComputerName% -4

Pinging CTH-0098 [192.168.56.1] with 32 bytes of data:
Reply from 192.168.56.1: bytes=32 time<1ms TTL=128
Reply from 192.168.56.1: bytes=32 time<1ms TTL=128
Reply from 192.168.56.1: bytes=32 time<1ms TTL=128
Reply from 192.168.56.1: bytes=32 time<1ms TTL=128

但我的实际系统 IP 地址是192.168.0.100

如何捕获此 IP 地址?

标签: batch-filevirtualboxip-address

解决方案


如果您有多个界面,ping则无济于事。该netsh命令为您提供了更多的灵活性:

for /f "delims=" %%a in ('netsh interface ipv4 show addresses name^="Local Area Connection" ^|find "IP"') do for %%b in (%%a) do set "ip=%%b"
echo Your IP is: %ip%

更改Local Area Connection为您的实际接口名称(使用 查找确切名称netsh interface show interface


推荐阅读