lundi 4 novembre 2019

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.

The IP can be found with the command (ipconfig | findstr IPv4 ). but

There are multiple internet adapters and multiple IP addresses

But I want to get only one address like 192.168.40.??

192.168.56.1 and 192.168.56.2 is my virtual box adapter

Not sure how to set the condition in the if statement.

I can not speak English well. Thanks for reading.

this is windows batch file (*.bat) ifconfig, findstr

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

=====start_pf.bat========

@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==
      rem my error start
      if _4octet==192.168.40.*  
netsh interface portproxy add v4tov4 listenport=8080 listenaddress=!_4octet! connectport=8080 connectaddress=192.168.56.1
      else echo not 40
      rem my error stop
      echo ========================
      netsh interface portproxy show v4tov4
     )
    )
  )
endlocal


=192.168.40.* was not expected.

Aucun commentaire:

Enregistrer un commentaire