这将打印的IP地址中的ipconfig
输出:
@echo off
set ip_address_string="IP Address"
rem Uncomment the following line when using Windows 7 (with removing "rem")!
rem set ip_address_string="IPv4 Address"
echo Network Connection Test
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do echo Your IP Address is: %%f
仅打印第一个IP地址,刚过添加goto :eof
(或其他标签跳转到的,而不是:eof
)回声或以更易读的形式:
set ip_address_string="IP Address"
rem Uncomment the following line when using Windows 7 or Windows 8/8.1 (with removing "rem")!
rem set ip_address_string="IPv4 Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
echo Your IP Address is: %%f
goto :eof
)
更可配置的方法是实际解析的输出一点点,这样你甚至可以指定要其IP地址的适配器:
@echo off
setlocal enabledelayedexpansion
::just a sample adapter here:
set "adapter=Ethernet adapter VirtualBox Host-Only Network"
set adapterfound=false
echo Network Connection Test
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do (
set "item=%%f"
if /i "!item!"=="!adapter!" (
set adapterfound=true
) else if not "!item!"=="!item:IP Address=!" if "!adapterfound!"=="true" (
echo Your IP Address is: %%g
set adapterfound=false
)
)
你可以有多个IP地址,大多数机器甚至有多个IP地址。那么你想要哪一个? – Joey 2011-05-05 20:50:43