我可以创建一个只提取多个 IP 中的一个的批处理程序吗?

Can I create a batch program that extracts only one of several IP?

我正在尝试创建一个端口转发我的 IP 的批处理文件。 IP可以用命令ipconfig | findstr IPv4查到,但是有多个网卡和多个IP地址。我只想获得一个地址,例如 192.168.40.??

192.168.56.1 和 192.168.56.2 是我的 VirtualBox 适配器。我不确定如何在 if 语句中设置条件。

这是 Windows 批处理文件 (*.bat):

ipconfig, findstr:

D:\>ipconfig | findstr IPv4
   IPv4 adress . . . . . . . . . : 192.168.56.2
   IPv4 adress . . . . . . . . . : 192.168.56.1
   IPv4 adress . . . . . . . . . : 192.168.40.19

我尝试了以下批处理文件:

@echo off
setlocal
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr IPv4`) do (
  for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
    for /f "tokens=1-4 delims=." %%c in ("%%b") do (
      set _o1=%%c
      set _o2=%%d
      set _o3=%%e
      set _o4=%%f
      set _4octet=!_o1:~1!.!_o2!.!_o3!.!_o4!     
      echo ==start port fowarding==
      netsh interface portproxy add v4tov4 listenport=8080 listenaddress=!_4octet! connectport=8080 connectaddress=192.168.56.1
      echo ========================
      netsh interface portproxy show v4tov4
     )
    )
  )
endlocal

但这在我所有的适配器上运行:

address         port        address         port
--------------- ----------  --------------- ----------
192.168.56.2    8080        192.168.56.1    8080          <- not want   
192.168.56.1    8080        192.168.56.1    8080          <- not want
192.168.40.19   8080        192.168.56.1    8080          <- i want only

然后我尝试了:

@echo off
setlocal
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr IPv4`) do (
  for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
    for /f "tokens=1-4 delims=." %%c in ("%%b") do (
      set _o1=%%c
      set _o2=%%d
      set _o3=%%e
      set _o4=%%f
      set _4octet=!_o1:~1!.!_o2!.!_o3!.!_o4!     
      echo ==start port fowarding==
      if !_4octet!==192.168.56.1
      (
      )
      else if !_4octet!==192.168.56.2
      (
      )
      else
      netsh interface portproxy add v4tov4 listenport=8080 listenaddress=!_4octet! connectport=8080 connectaddress=192.168.56.1
      echo ========================
      netsh interface portproxy show v4tov4
     )
    )
  )
endlocal

但我收到错误消息:

The syntax of the command is incorrect.

可能是变量(!_4octet!)没有正常工作的原因。

  if !_4octet! == 192.168.56.1 (

你能帮我解决这个问题吗?

如果你想得到这样的ip192.168.40.*

您可以像这样在命令 findstr 中使用一些正则表达式:

ipconfig | findstr IPv4 | findstr /r /c:"192.168.40.*"

您可以在此处查看更多帮助findstr /?

/R /C:string Use string as a regular expression.

因此,使用批处理脚本,您可以像这样将您的 IP 设置为变量:

@echo off
SetLocal EnableDelayedExpansion
for /f "usebackq delims=: tokens=2" %%a in (`ipconfig ^| findstr IPv4 ^| findstr /r /c:"192.168.40.*"`) do (
    Set "MyIP=%%a"
    Set "MyIP=!MyIP!"
    set "MyIP=!MyIP: =!" 
)
echo MyIP="!MyIP!"
pause

您可以使用 检索 IP 地址:

@Set "IPA="&For /F Tokens^=2Delims^=^" %%A In ('^""%__AppDir__%wbem\WMIC.exe"^
 NICConfig Where "IPEnabled='TRUE' And Not Description Like '%%VirtualBox%%'"^
 Get IPAddress 2^>NUL^"')Do @Set "IPA=%%A"
@If Defined IPA Echo=%IPA%&Pause>NUL