2

I'm trying to make a batch script to get local IP-address of active NIC (could be wireless or local). To get IP adress I have used following line:

FOR /F "delims=[] tokens=2" %%a in ('ping -4 %computername% -n 1 ^| findstr "["') do (set thisip=%%a)

I then have the IP address. The range is depending of the second group (eg. 64 in 172.64.10.10). I would like to launch a script if the local IP address is within 64 - 127 in the second group.

How can I do that?


I already tried to export the value in the second group but then I must export two or three characters:

SET IP=%thisip:~3,2%

Then I have managed to loop through the numbers but I am thinking wrong I don´t want to run the script more then once and my loop runs several times depending of what the IP Adress is:

:MyLoop
IF "%IP%" LSS "64" GOTO EndLoop
IF "%IP%" GTR "99" GOTO EndLoop
ECHO %IP%
SET /A IP+=1
GOTO MyLoop
:EndLoop
1
  • I have tried to do as follow to check the different criterias but it does allways print the "You´re NOT within the range!", why is that? code @ECHO OFF FOR /F "delims=[] tokens=2" %%a in ('ping -4 %computername% -n 1 ^| findstr "["') do (set thisip=%%a) SET IP=%thisip:~2,3% SET _result=%IP:.=% IF %_result% LSS "127" GOTO :ResultTrue IF %_result% GEQ "127" GOTO :ResultFalse :ResultTrue IF %_result% GEQ "64" GOTO :TrueResult :TrueResult ECHO %thisip% ECHO %IP% ECHO %_result% ECHO "You´re within the range!" :ResultFalse ECHO "You´re NOT within the range!" PAUSE Commented Oct 16, 2014 at 7:22

1 Answer 1

0

Here is the code you need, all in one batch file.

@echo off

setlocal EnableDelayedExpansion

set "thisIP="
set /a SecondGroup=0
set /a RangeStart=64
set /a RangeEnd=127

for /f "delims=[] tokens=2" %%A in ('ping -4 %computername% -n 1 ^| findstr "["') do (set thisIP=%%A)

for /f "delims=. tokens=1,2,3,4" %%A in ("!thisIP!") do set /a SecondGroup=%%B

if !SecondGroup! GEQ !RangeStart! (
  if !SecondGroup! LEQ !RangeEnd! (
    echo Let's execute you code here.
  ) else (
    echo IP is too high.
  )
) else (
  echo IP is too low.
)

setlocal DisableDelayedExpansion
endlocal

Remember to remove ECHO commands if you don't need them. I inserted them for clarification purpose only.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .