我环顾四周,似乎无法找到一个内部Windows版本的独立解决方案,以获取批处理文件中的计算机的IP地址。我想做的是,无论我使用的是什么样的Windows机器(无论是运行7或XP还是98甚至98),我都希望能够找出ip地址并将其存储到易变量中方式。
我可以使用ipconfig并解析出IPv4地址,但是Windows 7输出的内容与早期版本稍有不同,所以我首先要弄清楚它们的Windows版本,然后查找相应的字符串。任何帮助都会很棒!
答案 0 :(得分:7)
对于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%
@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:"
答案 1 :(得分: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%
答案 2 :(得分:1)
@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.
答案 3 :(得分:1)
与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%
同时返回public
和private IP