我有一个奇怪的问题,不确定是否可能。
我想写一个脚本,例如,我将使用ipconfig作为我的命令。
现在,当你正常运行这个命令时,会有大量的输出。
我想要的是一个脚本,将只显示IP地址,例如。
echo Network Connection Test
ipconfig <---This would run in the background
echo Your IP Address is: (INSERT IP ADDRESS HERE)输出将是
Network Connection Test
Your IP Address is: 192.168.1.1这有可能吗?
发布于 2011-05-06 01:45:36
这将在ipconfig的输出中打印IP地址
@echo off
set ip_address_string="IPv4 Address"
rem Uncomment the following line when using older versions of Windows without IPv6 support (by removing "rem")
rem set ip_address_string="IP 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="IPv4 Address"
rem Uncomment the following line when using older versions of Windows without IPv6 support (by removing "rem")
rem set ip_address_string="IP Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
echo Your IP Address is: %%f
goto :eof
)一种更具可配置性的方法是实际解析一下ipconfig /all的输出,这样您甚至可以指定想要其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
)
)发布于 2013-07-14 04:36:22
下面的代码可以在从Windows XP开始的任何平台的任何区域设置上工作,它会从(或多或少)随机的网卡中查找网络IP。它永远不会超过几毫秒。
for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a
echo Network IP: %NetworkIP%下面的代码将查找您的公网IP,并在Windows7及更新版本的机器上运行。
for /f %%a in ('powershell Invoke-RestMethod api.ipify.org') do set PublicIP=%%a
echo Public IP: %PublicIP% 您可以在my blog上找到这些命令的详细说明。
发布于 2012-12-23 07:18:29
在Windows 7中:
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
echo %ip%
pausehttps://stackoverflow.com/questions/5898763
复制相似问题