function – 批处理脚本中的IP验证 – 先通过findstr匹配,然后通
|
这是批量专业人士的问题.似乎很多人在批处理时偶然发现IP验证,而只是使用内置功能的Windows,但没有真正的代码可以找到.
在几个地方可以找到findstr表达式,以识别匹配四个数字序列的数字字符串. findstr /r "[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*" 虽然似乎没有更好的方法来识别字符串本身(因为对于findstr的正则表达式支持有限),但它会捕获过于误报,如1234.2.3.4或999.999.999.999,当然还有0.0.0.0. 它“仅”需要进一步验证找到的字符串,例如使用for循环序列,并且`make shure每个找到的字符串八位字节对IP规则有效. > 1到254之间的第一个八位字节 如果有人将第二部分验证集成到此代码中以进一步确定是否发现IP是3个私有类之一(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完成一个交换机,以选择是否应验证私有IP或公共IP,并且返回值可以告知更接近哪种类型甚至子网掩码已被识别: >第一个八位字节必须是255 总的来说这将是伪例程: 一个函数接受输入,无论来自哪里,只是被调用,接受使用和返回结果的开关 > take用于私有或公共IP范围的交换机 > IP验证 – 识别范围和设置返回值 >私人或 >设置returnvalue,根据开关设置errorlevel 如果代码确实设置了很好的可用返回代码(例如,返回识别的ip范围),那么对于每个处理IP4的人来说,这将是一个alltime函数.虽然我将自己扩展这些ip范围,但如果只是函数将可靠地返回那些“_return”值,例如示例代码. 我忘了什么吗? 还没有人编码吗? 解: @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,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%
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- .NET中分离的项目/ DLL的缺点和优点?他们中有多少是太多了
- windows-server-2012 – Windows Server 2012上的多个CA.
- 代码覆盖 – 是否有适用于通用Windows软件包应用程序的代码
- .net – NServiceBus Bus.Send().注册(回调)在IIS / Window
- windows-8 – Windows 8 JavaScript应用程序开发 – 共享魅
- iis-7 – 可以在XP上安装IIS7吗?
- windows-server-2012 – 来自远程桌面会话的远程协助:无法
- 10、管理站点复制
- Windows批处理文件’del’无效
- windows – Qt Creator使用MinGW编译器和CMake
- 使用fopen增加Windows中Matlab中打开文件描述符的
- windows – 为什么某些以太网卡上的TCP Chimney
- Windows Server 2008搭建web服务
- Windows Phone 7 – 在NavigationService中清除后
- XAML和Windows Mobile(.Net Compact Framework)
- 找不到wpcap.dll 解决方案
- 在Windows中运行/调试python程序的最简单的方法
- windows-server-2008 – 将VMware Server 2.0映像
- 图标 – 在Windows资源管理器中显示exe的图标时,
- .net – HttpListener.Start()Vista中的AccessDe
