IP verification in batch script - first match by findstr, secondly verify by for loops (only using windows built in functinallity?

前端 未结 4 1957
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 23:36

This is a question for the batch pro\'s i guess. Seems a lot of people do stumble over IP veriffication while batching, while just using windows built in functinallity, but

4条回答
  •  再見小時候
    2021-01-07 00:27

    This is a NEW answer to the NEW question in this same topic!

    As I said you in my comment I need to understand what is the supposed operation of the code in order to fix it (otherwise, how could I do that?), but you not gave me a single description of your NEW code, so I can only guess...

    So I guess that :validateIP subroutine must return a numeric errorlevel value to the caller program as described in exit /? command, and that it optionally return a string variable to the caller's environment. The code below do that:

        :validateIP ipAddress [/ipRange] [returnVariable]
    
        rem prepare environment
        setlocal 
    
        rem Initialize ip range as public
        set ipCASE=public
    
        rem Process switches
        set "returnVar=%~2"
        rem If second parameter start with slash...
        if "%returnVar:~0,1%" equ "/" (
            rem It is the /ipRange
            set "ipCASE=%returnVar:~1%"
            set "returnVar=%~3"
        )
    
    echo ipcase: %ipCase%
    
        rem asume failure in tests : 0=pass 1=fail : same for return/errorlevel
        set "_return=1"
        set "_returnlevel=1"
    
        rem test if address conforms to ip address structure
        echo %~1| findstr /b /e /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" >nul
    
        rem if it conforms to structure, test each octet for range values
        if not errorlevel 1 for /f "tokens=1-4 delims=." %%a in ("%~1") do (
            if %%a gtr 0 if %%a lss 255 if %%b leq 255 if %%c leq 255 if %%d gtr 0 if %%d leq 254 set "_return=public"
            if %%a equ 10 if %%b geq 0 if %%b lss 255 if %%c geq 0 if %%c lss 255 if %%d gtr 0 if %%d leq 254 set "_return=private"
            if %%a equ 172 if %%b geq 16 if %%b lss 31 if %%c geq 0 if %%c lss 255 if %%d gtr 0 if %%d leq 254 set "_return=private"
            if %%a equ 192 if %%b equ 168 if %%c geq 0 if %%c lss 255 if %%d gtr 0 if %%d leq 254 set "_return=private"
            if %%a equ 127 if %%b geq 0 if %%b lss 255 if %%c geq 0 if %%c lss 255 if %%d gtr 0 if %%d leq 254 set "_return=local"
        )
    
        rem set errorlevels
        if "%ipCASE%"=="public"  if "%_return%"=="public"  (set "_returnlevel=0") else (set "_returnlevel=1")
    
        if "%ipCASE%"=="private" if "%_return%"=="private" (set "_returnlevel=0") else (set "_returnlevel=1")
    
        :endValidateIP
        rem clean and return data/errorlevel to caller
        endlocal & ( if not "%returnVar%"=="" set "%returnVar%=%_return%" ) & exit /b %_returnlevel%     
    

    However, when I tested this subroutine with your testing code I got strange results. I reviewed the entire code and discovered several problems not in the subroutine, but in the testing code; for example, echo %errorlevel% inside the main for loop always will show 0, and the invocation of :validateIP ipAddress [/ipRange] [returnVariable] don't follow this format in 3 of 4 cases and sometimes don't include the return variable, but the calling code always show it, etc...

    Of course, these problems have no relation with the original theme of this topic nor with your last request and I already spent too much time in this matter, so I didn't solved they...

提交回复
热议问题