3

我环顾四周,似乎找不到内部 Windows 版本独立的解决方案来在批处理文件中获取计算机的 IP 地址。我想做的是,无论我在什么 Windows 机器上(无论是运行 win 7 还是 XP 甚至可能是 98),我都希望能够找出 IP 地址并将其存储到一个简单的变量中时尚。

我可以使用 ipconfig 并解析出 IPv4 地址,但 Windows 7 输出的内容与早期版本略有不同,因此我首先必须弄清楚它们拥有的 Windows 版本,然后查找适当的字符串。任何帮助都会很棒!

4

4 回答 4

7

XP Pro / Vista / 7 / 8:

对于 Windows XP 和更新版本,我建议使用WMIC.

@echo off
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
echo %IP%

98 / 2000 / XP 主页:

@echo off
for /f "tokens=2* delims=:" %%A in ('ipconfig /all ^| find "IP Address"') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
for /f "tokens=2* delims=:" %%A in ('ipconfig /all ^| find "IPv4 Address"') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
echo %IP%

其他命令

netsh interface ip show addresses

nbtstat -n | find "IpAddress:"

于 2013-05-29T15:25:09.347 回答
6

我想这会做到:

@echo off
FOR /F "tokens=2,3" %%A IN ('ping %computername% -n 1 -4') DO IF "from"== "%%A" set "IP=%%~B"
echo %IP:~0,-1%
于 2013-05-29T15:56:20.493 回答
1

Get your real internet IP Windows version independent with GNU wget

@echo off&setlocal
for /f %%i in ('wget ident.me --output-document=- 2^>nul') do set "myRealIP=%%i"
if defined myRealIP (echo Your real IP is stored in %myRealIP%) else echo Error! No connection to the internet.
于 2013-05-29T16:53:30.090 回答
1

This works fine with windows 10

@echo off
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
for /f "tokens=1 delims=:" %%j in ('ping %computername% -4 -n 1 ^| findstr Reply') do (
    set localip=%%j
)
echo Public IP is: %IP%
echo Local  IP is: %localip:~11%

Returns both public and private IP

于 2018-07-19T06:01:21.993 回答