batch-file - 使用“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 地址?
解决方案
如果您有多个界面,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
)
推荐阅读
- php - public_html/vendor/symfony/polyfill-php80/bootstrap.php 在第 23 行
- hazelcast - Hazelcast 中 CP 数据结构的会话超时
- python - 在 Python 中合并同一列
- flutter - 从 Firestore 集合中获取数据 snapshot.hasdata 返回 Null
- python-3.x - 如何添加分页:super()
- html - HTML/CSS 解决方法 menu-toggle 仅适用于第一个元素
- node.js - 在 $inc 中使用 $sub 并引用集合字段
- ios - xcode中不能选择Development Team,只显示Personal Team
- python - 根据其他行选择熊猫数据框行
- php - mySQL 查询返回根据'LIMIT' 使用而改变| 数据库