function - 批处理脚本中的 IP 验证 - 首先通过 findstr 进行匹配,然后通过 for 循环进行验证(仅使用内置功能的窗口?

标签 function batch-file for-loop verification findstr

我猜这是批处理专家的问题。似乎很多人在批处理时确实被 IP 验证绊倒了,而只是使用内置功能的窗口,但找不到真正的代码。

在几个地方可以找到 findstr 表达式,以识别匹配四个数字序列的数字字符串。

findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"

虽然似乎没有更好的方法来识别字符串本身(因为 findstr 的正则表达式支持有限),但它会捕获太多误报,例如 1234.2.3.4999.999.999.999 当然还有 0.0.0.0

它“只”需要进一步验证找到的字符串,例如使用一系列 for 循环,并确保每个找到的字符串八位字节对 IP 规则有效。

  • 1 和 254 之间的第一个八位字节
  • 第二和
  • 第三个介于 0 和 255
  • 1 和 254 之间

如果有人将验证的第二部分集成到此代码中,以进一步识别找到的 IP 是否是 3 个私有(private)类之一(10.0.0.0/8、172.16.0.0/12 和 192.168.0.0/16 或更确切地说是以下之一:10.0.0.0 到 10.255.255.255、172.16.0.0 到 172.31.255.255 和 192.168.0.0 到 192.168.255.255)这将生成一个循环函数。不要忘记必须警告特殊的 127.x.x.x D)。

要完成 IP4 切换以选择是否验证私有(private) IP 或公共(public) IP,并且返回值可以更清楚地告诉您识别了哪种类型甚至子网掩码:

  • 第一个八位字节必须是 255
  • 第二和
  • 第三和
  • 0,128,192,224,240,248,252,254,255 的第四个

所以总的来说这将是伪例程:

一个接受输入的函数,无论来自哪里,只是被调用,使用开关并返回结果

  • 使用私有(private)或公共(public) IP 范围的交换机
  • 验证 IP 语法
    • IP验证-识别范围和设置返回值
      • 私有(private)或
      • 公共(public)课
      • 子网
      • ...
  • 设置返回值,根据switch设置errorlevel

如果代码确实设置了很好的可用返回代码(例如,返回识别的 ip 范围),这将是每个处理 IP4 的人的全天候功能。虽然我会自己扩展这些 ip 范围,但只要函数能够像示例代码那样可靠地返回那些“_return”值。

我是不是忘记了什么?

没有人编写过这个代码吗?

解决方案: 根据 MC ND 的示例和 Aacini 的开关处理和子网掩码代码,我修改了代码,添加了错误处理使用 echo 和其他情况 - 这里包括一些测试示例代码:

  @echo off
          setlocal enableextensions enabledelayedexpansion

          rem try some ip addresses 
          for %%i in ("1.2.3.4" "0.1.2.3" "250.1024.1.2" "10.0.2.1" "127.0.0.1" "1.2.3.255" "172.16.17.18" "192.168.1.1" "255.128.240.0" "0.0.0.0" "something" "" ) do (
          REM 1.2.3.4 is public / 0.1.2.3 is false all / 10.0.2.1 is private / 127.0.0.1 is local / 172.16.17.18 is private / 192.168.1.1 is private / 255.128.240.0 is subnet / 0.0.0.0 is false all (source net)


              echo --------------- run one as default case assuming pulic with ret var -------------------

              rem call default with a return variable 
              call :validateIP %%~i ret && echo %%i is valid || echo %%i is invalid
              echo return value: !ret!
              echo --------------------------------------------

              echo --------------- run two with switch public -------------------

              rem call with switch public 
              call :validateIP %%~i /public && echo %%i is valid || echo %%i is invalid
              echo return value: !ret!
              echo --------------------------------------------

              echo ------------ run three with switch private ---------------------
              rem call with switch private 
              call :validateIP %%~i /private && echo %%i is valid || echo %%i is invalid
              echo return value: !ret!
              echo --------------------------------------------

              echo ------------ run four with switch private and ret variable ---------------------
              rem call with switch private and return variable
              call :validateIP %%~i /private ret && echo %%i is valid || echo %%i is invalid
              echo return value: !ret!
              echo --------------------------------------------

              echo ------------ run five with switch local and ret variable ---------------------
              rem call with switch private and return variable
              call :validateIP %%~i /local ret && echo %%i is valid || echo %%i is invalid
              echo return value: !ret!
              echo --------------------------------------------

              echo ------------ run six with switch subnet and ret variable ---------------------
              rem call with switch private and return variable
              call :validateIP %%~i /subnet ret && echo %%i is valid || echo %%i is invalid
              echo return value: !ret!
              echo --------------------------------------------

              echo ------------ run seven with switch source and ret variable ---------------------
              rem call with switch private and return variable
              call :validateIP %%~i /source ret && echo %%i is valid || echo %%i is invalid
              echo return value: !ret!
              echo --------------------------------------------

              echo ------------ run eight with nothing ---------------------
              rem call with switch private and return variable
              call :validateIP && echo is valid || echo is invalid
              echo return value: !ret!
              echo --------------------------------------------

          )   
          exit /b 


 :validateIP ipAddress [/ipRange] [returnVariable]

  rem prepare environment
  setlocal enableextensions enabledelayedexpansion 


  if "%~1"=="" goto USAGE
  echo %~1| findstr /b /e /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" >nul
  if errorlevel 1 goto USAGE
  :afterusage

  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"
  )

  rem asume failure in tests : 0=pass 1=fail : same for return/errorlevel
  set "_return=1"
  set "_returnlevel=1"
  set "subNETNumbers=0,128,192,224,240,248,252,254,255"

  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"
      if %%a equ 255 if not "!subNETNumbers:%%b=!" equ "%subNETNumbers%" if not "!subNETNumbers:%%c=!" equ "%subNETNumbers%" if not "!subNETNumbers:%%d=!" equ "%subNETNumbers%" set "_return=subnetmask"
      if %%a equ 0 set "_return=sourcenetwork"
  )

  rem set returnlevels depending on given switch
  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")

  if "%ipCASE%"=="local" if "%_return%"=="local" (set "_returnlevel=0") else (set "_returnlevel=1")

  if "%ipCASE%"=="subnet" if "%_return%"=="subnetmask" (set "_returnlevel=0") else (set "_returnlevel=1")

  if "%ipCASE%"=="source" if "%_return%"=="sourcenetwork" (set "_returnlevel=0") else (set "_returnlevel=1")

  REM OPTION1 set errorlevel
  REM another correct way to set errorlevel would be to REM this line beneath and instead use _returnlevel with exit /b like in line REM OPTION2 - while this is interesting way to set it indirectly
  if "%_returnlevel%"=="0" (ver > nul) else (set dummy 2> nul)


  :endValidateIP
  rem clean and return data/errorlevel to caller
  endlocal & ( if not "%returnVar%"=="" set "%returnVar%=%_return%" ) & exit /b 
  REM OPTION2 endlocal & ( if not "%returnVar%"=="" set "%returnVar%=%_return%" ) & exit /b %_returnlevel%  

  :usage
  echo.
  echo   Usage:  call :validateIP [/ipRange] [returnVariable]
  echo.   
  echo        for example: call :validateIP 127.0.0.2 /local ret 
  echo.   
  echo     if NO switch is given function assumes public, 
  echo     switch and return var are optional
  echo     errorlevel depends and corresponds on given switch
  echo     known switches: /public, /private, /local, /subnet, /source
  echo     return var reflects syntax check, if return var is "1" the input was malformed anyhow
  echo.
  goto :afterusage

最佳答案

IP 验证的基本结构。根据需要进行调整

@echo off
    setlocal enableextensions enabledelayedexpansion

    rem try some ip addresses 
    for %%i in ("1.1.1.1" "0.1.1.1" "250.1024.1.1" "10.0.2.1" "something" "" ) do (

        echo --------------------------------------------

        rem call with a variable to get return value
        call :validateIP %%~i ret 
        echo %%~i : return value : !ret! 

        rem call with or without variable to get errorlevel
        call :validateIP %%~i  && echo %%i is valid || echo %%i is invalid

    )   

    exit /b 

:validateIP ipAddress [returnVariable]
    rem prepare environment
    setlocal 

    rem asume failure in tests : 0=pass 1=fail : same for errorlevel
    set "_return=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 rage 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=0"
    )

:endValidateIP
    rem clean and return data/errorlevel to caller
    endlocal & ( if not "%~2"=="" set "%~2=%_return%" ) & exit /b %_return%

关于function - 批处理脚本中的 IP 验证 - 首先通过 findstr 进行匹配,然后通过 for 循环进行验证(仅使用内置功能的窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20299266/

相关文章:

javascript - For..in 循环无法设置未定义的属性

c++ - 如何检查一个函数是否被另一个函数调用?

batch-file - 如何将 "cd"逐步放入文本文件中的所有文件夹中?

python - 如何在单行中使用 for 循环将这些列表拆分为列表?

mysql - 如何使用for循环从mysql表中读取数据

batch-file - 启动新的 cmd.exe 并且不继承环境?

javascript - setInterval() 同时执行的两个函数会互相减慢

javascript - JS 代码中的加号 - 为什么它们在这里不是可选的

sql - T SQL如何通过删除最后一个分隔符之后的所有部分来修改字符串

python - 使用外部 python 脚本打开 maya 并在 maya 中运行另一个脚本