在批处理脚本IP验证 - 通过FINDSTR的第一场比赛,其次为循环(仅使用内置functinallity窗口核实? [英] IP verification in batch script - first match by findstr, secondly verify by for loops (only using windows built in functinallity?

查看:321
本文介绍了在批处理脚本IP验证 - 通过FINDSTR的第一场比赛,其次为循环(仅使用内置functinallity窗口核实?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是批处理亲的我猜的问题。看来很多人都绊倒IP veriffication而配料,而只使用内置的functinallity窗户,但没有真正的code是找到。

在几个地方一FINDSTR前pression玛丽萨是,确定一个数字串匹配的号码的四个序列。

  FINDSTR / R[0-9] [0-9] * \\。[0-9] [0-9] * \\。[0-9] [0- 9] * \\。[0-9] [0-9] *

虽然似乎没有更好的办法来识别字符串本身(因为经常EX pression支持FINDSTR有限的)这捕获过误报像 1234.2.3.4 999.999.999.999 当然 0.0.0.0

这将只需要进一步核实中发现的字符串,例如与for循环序列,使`舒尔每发现弦乐八是有效的IP规则。


  • 第一个和八位字节1 254

  • 第二和

  • 之间的第三 0和255

  • 1和254之间的提出

如果一会又验证的第二部分融入这个code,以进一步确定是否找到IP是3个私人班( 10.0.0.0/8,172.16.0.0/12和192.168.0.0/16 或者是precice之一: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完成一个开关来选择是否私有或公有IP应当得到验证,returnvalues​​可能会告诉接近已被确认是什么样,甚至子网掩码:


  • 第一个字节必须是 255

  • 第二和

  • 第三和

  • 0,128,192,224,240,248,252,254,255 的第四个

所以,总体来说,这将是伪常规:

服用输入,无论身在何处而来,只是为了被调用,以开关的使用和返回结果的函数


  • 采取了开关私有或公有IP地址范围

  • 验证IP语法

    • IP验证 - 认可的范围,并设置返回值

      • 私人或

      • 推进公众类

      • 子网

      • ...



  • 设置返回值,这取决于开关设置ERRORLEVEL

如果在code确实设置好的回报可用codeS(如例子,返回公认的IP范围),这将是对每个人处理IP4的反正一alltime功能。虽然我将扩展这些IP范围喽,如果仅仅只是函数将返回可靠那些_return - 值就像例如code。

我也不曾忘记什么?

没有人响应code这个了吗?

解决方案:
根据MC ND的榜样,Aacini的开关操作和子网掩码$ C C I炸毁了code,补充ErrorHandling中使用的回声和其他情况 - 一些code测试样本包括在这里:

 关闭@echo
          SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion          REM尝试一些IP地址
          为%%我在(1.2.3.4,0.1.2.3,250.1024.1.210.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东西)做(
          REM 1.2.3.4是公共/ 0.1.2.3是假的一切/ 10.0.2.1是私有/ 127.0.0.1是本地/ 172.16.17.18是私有/ 192.168.1.1是私有/ 255.128.240.0为子网/ 0.0.0.0是假的一切(来源网络)
              回声---------------运行一个作为默认的情况下,假设与RET VAR推进公众-------------------              REM调用默认与返回变量
              拨打:validateIP %%〜我沤&放大器;&安培;回声%% i被有效||呼应%% i是无效
              回声返回值:退役!
              回声--------------------------------------------              回声---------------运行两次与开关公共-------------------              REM电话与公众开关
              拨打:validateIP %%〜I /公共和放大器;&安培;回声%% i被有效||呼应%% i是无效
              回声返回值:退役!
              回声--------------------------------------------              ------------回声带开关的私人运行三个---------------------
              REM调用带开关的私人
              拨打:validateIP %%〜I /私人和放大器;&安培;回声%% i被有效||呼应%% i是无效
              回声返回值:退役!
              回声--------------------------------------------              回声------------运行四个带开关的私人和RET变量---------------------
              REM调用带开关的私人和返回变量
              拨打:validateIP %%〜I /私人RET和放大器;&安培;回声%% i被有效||呼应%% i是无效
              回声返回值:退役!
              回声--------------------------------------------              回声------------运行五连开关本地和RET变量---------------------
              REM调用带开关的私人和返回变量
              拨打:validateIP %%〜I /本地RET和放大器;&安培;回声%% i被有效||呼应%% i是无效
              回声返回值:退役!
              回声--------------------------------------------              ------------回声带开关的子网和RET变量管理6个---------------------
              REM调用带开关的私人和返回变量
              拨打:validateIP %%〜I /子网RET和放大器;&安培;回声%% i被有效||呼应%% i是无效
              回声返回值:退役!
              回声--------------------------------------------              回声------------运行七连开关源和RET变量---------------------
              REM调用带开关的私人和返回变量
              拨打:validateIP %%〜I /源RET和放大器;&安培;回声%% i被有效||呼应%% i是无效
              回声返回值:退役!
              回声--------------------------------------------              回声------------没事运行8个---------------------
              REM调用带开关的私人和返回变量
              致电:validateIP&功放;&安培;回声是有效的||回声是无效的
              回声返回值:退役!
              回声--------------------------------------------          )
          退出/ B
 :validateIP ip地址[/ ipRange] [returnVariable]  REM prepare环境
  SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
  如果%〜1==goto语句用法
  回声%〜1 | FINDSTR / B / E / R[0-9] [0-9] * \\。[0-9] [0-9] * \\。[0-9] [0-9] * \\。[0- 9] [0-9] *> NUL
  如果ERRORLEVEL 1 goto语句用法
  :afterusage  REM初始化IP范围作为公共
  设置ipCASE =公  REM进程切换
  集returnVar =%〜2
  REM如果第二个参数开始斜线...
  如果%returnVar:〜0.1%EQU/(
      REM它是/ ipRange
      设置ipCASE =%returnVar:〜1%。
      集returnVar =%〜3
  )  REM asume失败测试:0 =通过1 =失败:对于相同的回报/错误级别
  集_return = 1
  集_returnlevel = 1
  设置subNETNumbers = 0,128,192,224,240,248,252,254,255  REM测试,如果地址符合IP地址结构
  回声%〜1 | FINDSTR / B / E / R[0-9] [0-9] * \\。[0-9] [0-9] * \\。[0-9] [0-9] * \\。[0- 9] [0-9] *> NUL  REM若符合结构,测试每个字节的值范围
  如果不是ERRORLEVEL 1 / F令牌= 1-4 delims =。 %%一个在(%〜1)做(
      如果%%一个GTR 0,如果一个%% LSS 255 %%,如果当量b如果255 %%ç当量255 %%,如果GTRÐ0,如果%%ð当量254集_return =公
      如果%%一个EQU 10 %%,如果GEQ b 0,如果%% b LSS 255 %%,如果ÇGEQ 0,如果%%çLSS 255 %%,如果GTRÐ0,如果%%ð当量254集_return =私人
      如果%%一个EQU 172 %%,如果GEQ b如果16 %% b LSS 31 %%,如果ÇGEQ 0,如果%%çLSS 255 %%,如果GTRÐ0,如果%%ð当量254集_return =私人
      如果%%一个EQU 192 %%,如果EQU b如果168 %%çGEQ 0,如果%%çLSS 255 %%,如果GTRÐ0,如果%%ð当量254集_return =私人
      如果%%一个EQU 127 %%,如果GEQ b 0,如果%% b LSS 255 %%,如果ÇGEQ 0,如果%%çLSS 255 %%,如果GTRÐ0,如果%%ð当量254集_return =本地
      如果%%一个EQU 255如果不是subNETNumbers:!%% B = EQU%subNETNumbers%如果不是subNETNumbers:%% C =! EQU%subNETNumbers%如果不是subNETNumbers:%% D =! EQU%subNETNumbers%的设置_return =子网掩码
      如果%%一个EQU 0组_return = sourcenetwork
  )  REM SET returnlevels根据给定的开关
  如果%ipCASE%==公开%_return%==公开(集_returnlevel = 0),其他(SET_returnlevel = 1)  如果%ipCASE%==私人如果%_return%==私人(集_returnlevel = 0),其他(SET_returnlevel = 1)  如果%ipCASE%==本地如果%_return%==本地(集_returnlevel = 0),其他(SET_returnlevel = 1)  如果%ipCASE%==子网如果%_return%==子网掩码(集_returnlevel = 0),其他(SET_returnlevel = 1)  如果%ipCASE%==源如果%_return%==sourcenetwork(集_returnlevel = 0),其他(SET_returnlevel = 1)  REM OPTION1设置错误级别
  REM另一种正确的方式来设置错误级别将REM这条线之下,而不是行REM OPTION2使用_returnlevel,退出/ b类似 - 而这是设置间接有趣的方式
  如果%_returnlevel%==0(第二版> NUL)否则(设置虚设2 - ; NUL)
  :endValidateIP
  REM干净返回数据/错误级别呼叫者
  ENDLOCAL&安培; (如果不是%returnVar%==设置%returnVar%=%_回报%)及退出/ B
  REM OPTION2 ENDLOCAL&安培; (如果不是%returnVar%==设置%returnVar%=%_回报%)及退出/ B%_returnlevel%  :用法
  回声。
  回声使用方法:拨打:validateIP [/ ipRange] [returnVariable]
  回声。
  回声例如:拨打:validateIP 127.0.0.2 /本地RET
  回声。
  回声如果没有给出切换功能假定公众,
  回声开关和返回VAR是可选
  回声错误级别依赖,并给予交换机上对应
  回声称为开关:/公众/私人/地方,/子网/源
  回波VAR反映语法检查,如果返回VAR为1的输入被无论如何畸形
  回声。
  转到:afterusage


解决方案

有关IP验证基本结构。适应需要

 关闭@echo
    SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion    REM尝试一些IP地址
    为%%我在(1.1.1.1,0.1.1.1,250.1024.1.110.0.2.1东西)做(        回声--------------------------------------------        REM调用使用变量获得返回值
        拨打:validateIP %%〜我沤
        呼应%%〜我:返回值:退役!        REM调用带或不带变量获得ERRORLEVEL
        拨打:validateIP %%〜I和;&安培;回声%% i被有效||呼应%% i是无效    )    退出/ B:validateIP ip地址[returnVariable]
    REM prepare环境
    SETLOCAL    REM asume失败测试:0 =通过1 =失败:同样的错误级别
    集_return = 1    REM测试,如果地址符合IP地址结构
    回声%〜1 ^ | FINDSTR / B / E / R[0-9] [0-9] * \\。[0-9] [0-9] * \\。[0-9] [0-9] * \\。[0- 9] [0-9] *> NUL    REM若符合结构,测试每个字节的怒气值
    如果不是ERRORLEVEL 1 / F令牌= 1-4 delims =。 %%一个在(%〜1)做(
        如果%%一个GTR 0,如果一个%% LSS 255 %%,如果当量b如果255 %%ç当量255 %%,如果GTRÐ0,如果%%ð当量254集_return = 0
    ):endValidateIP
    REM干净返回数据/错误级别呼叫者
    ENDLOCAL&安培; (如果不是%〜2==设置%〜2 =%_回报%)及退出/ B%_return%

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 no real code is to find.

At several places a findstr expression is findable, to identify a numerical string matching four sequences of numbers.

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

While there seems no better way to identify the string itself (because of limited regular expression support of findstr) this catches too false positives like 1234.2.3.4 or 999.999.999.999 and of course 0.0.0.0.

It would "only" need to verify the found string further, e.g. with a sequence of for loops, and `make shure each found string octet is valid for IP rules.

  • first octet between 1 and 254
  • second and
  • third between 0 and 255
  • forth between 1 and 254

If one would then integrate a second part of verification into this code to identify further if found IP was one of the 3 private classes (10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 or to be precice one of: 10.0.0.0 to 10.255.255.255, 172.16.0.0 to 172.31.255.255 and 192.168.0.0 to 192.168.255.255) this would make a round function. And not to forget the special 127.x.x.x must get warned D).

To get IP4 complete a switch to choose if private or public IP shall get verified and returnvalues may tell closer what kind or even subnet masks have been recognized:

  • first octet must be 255
  • second and
  • third and
  • fourth one of 0,128,192,224,240,248,252,254,255

So overall this would be the pseudo routine:

a function taking input, no matter where from, just to be called, taking switch for usage and returning result

  • take Switches for private or public IP range
  • validate IP Syntax
    • IP verification - recognized range and set return values
      • private or
      • pulic class
      • subnet
      • ...
  • set returnvalue, set errorlevel depending on switch

If the code does set nice usable return codes (like in example, return the recognized ip range) this would be an alltime function for everybody dealing with IP4's anyway. While i will extend those ip ranges myself, if only just the function will reliable return those "_return"-values like in example code.

Did i forget something?

Nobody did code this already?

SOLUTION: According to MC ND's example and Aacini's switch handling and subnetmask code i blew up the code, added errorhandling usage echo and other cases - here with some testing sample code included:

  @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

解决方案

Basic structure for ip validation. Adapt as needed

@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%

这篇关于在批处理脚本IP验证 - 通过FINDSTR的第一场比赛,其次为循环(仅使用内置functinallity窗口核实?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆