简体   繁体   English

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

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

I am trying to create a batch file that port forwards my IP.我正在尝试创建一个端口转发我的 IP 的批处理文件。 The IP can be found with the command ipconfig | findstr IPv4 IP 可以通过命令ipconfig | findstr IPv4找到。 ipconfig | findstr IPv4 , but there are multiple internet adapters and multiple IP addresses. ipconfig | findstr IPv4 ,但有多个互联网适配器和多个 IP 地址。 I want to get only one address like 192.168.40.??我只想得到一个地址,比如192.168.40.??

192.168.56.1 and 192.168.56.2 is my VirtualBox adapter. 192.168.56.1 和 192.168.56.2 是我的 VirtualBox 适配器。 I'm not sure how to set the condition in the if statement.我不确定如何在 if 语句中设置条件。

This is the Windows batch file (*.bat):这是 Windows 批处理文件 (*.bat):

ipconfig, findstr: ipconfig,findstr:

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

I tried the following batch file:我尝试了以下批处理文件:

@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

But this runs on all of my adapters:但这在我所有的适配器上运行:

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

I then tried:然后我尝试了:

@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

But I receive the error:但我收到错误:

The syntax of the command is incorrect.该命令的语法不正确。

Perhaps the cause is that the variable (._4octet!) Is not working properly.也许原因是变量 (._4octet!) 工作不正常。

  if !_4octet! == 192.168.56.1 (

Can you help me with this problem?你能帮我解决这个问题吗?

If you want to get something like this ip 192.168.40.*如果你想得到这样的东西 ip 192.168.40.*

You can use a little regex with the command findstr like this:您可以像这样在命令findstr中使用一些正则表达式:

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

You can see more help here findstr /?您可以在这里找到更多帮助findstr /?

/R /C:string Use string as a regular expression. /R /C:string 使用字符串作为正则表达式。

So, with a batch script you can do it to set your IP into Variable like this:因此,您可以使用批处理脚本将 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

You could use to retrieve the IP Address:您可以使用检索 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM