Validar endereços IP na entrada do usuário com script em lote

0

Related:
Script to assign and remove arbitrary secondary IPs to an interface

Estou tentando aumentar o script na pergunta acima, validando a entrada do usuário para que o script funcione somente com endereços IP e máscaras de sub-rede válidos. Estou ciente de que há um número de diferentes expressões regulares disponíveis que irão verificar IPs, mas eu não sei como (ou mesmo se houver uma maneira) para usar isso contra as variáveis no meu script em lotes com comandos nativos do Windows XP.

Alguém pode me indicar a direção certa aqui?

    
por Iszi 12.12.2011 / 20:02

2 respostas

1

Eu tenho mexido. Não me pergunte por que ... mas aqui está uma verificação extensa para o seu ipaddress / gateway / subnet-mask:

@Echo Off

set NEW-IPADDR=192.168.1.2
set NEW-MASK=255.255.255.240
set NEW-GW=192.168.1.1

set RETURN=isValidIP
goto checkIP

:isValidIP
echo.We are good to go.

REM ---------------------------------------------------------------------
REM Do whatever with the IP/mask/GW here.  The values appear to be valid.
REM ---------------------------------------------------------------------
goto :End

:checkIP
for /f "tokens=1,2,3,4 delims=. " %%a in ("%NEW-IPADDR%") do set ip1=%%a&set ip2=%%b&set ip3=%%c&set ip4=%%d
set /a decIP=(16777216*%ip1%)+(65536*%ip2%)+(256*%ip3%)+%ip4% 2> nil
for /f "tokens=1,2,3,4 delims=. " %%a in ("%NEW-MASK%") do set mask1=%%a&set mask2=%%b&set mask3=%%c&set mask4=%%d
set /a decMask=(16777216*%mask1%)+(65536*%mask2%)+(256*%mask3%)+%mask4% 2> nil
set /a netAddr="%decIP%&%decMask%" 2> nil
for /f "tokens=1,2,3,4 delims=. " %%a in ("%NEW-GW%") do set gw1=%%a&set gw2=%%b&set gw3=%%c&set gw4=%%d
set /a decGW=(16777216*%gw1%)+(65536*%gw2%)+(256*%gw3%)+%gw4% 2> nil
set /a gwNetAddr="%decGW%&%decMask%" 2> nil


set isBadLabel=badIP
if %ip1% EQU 127 (goto :badIP)
if %ip1% EQU 0 (goto :badIP)
set num=%ip1%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%ip2%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%ip3%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%ip4%
call :checkNum
if %badNum% equ 1 (goto :End)

set isBadLabel=badMask
set num=%mask1%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%mask2%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%mask3%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%mask4%
call :checkNum
if %badNum% equ 1 (goto :End)

set isBadLabel=badGW
set num=%gw1%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%gw2%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%gw3%
call :checkNum
if %badNum% equ 1 (goto :End)
set num=%gw4%
call :checkNum
if %badNum% equ 1 (goto :End)

set testmask=-2
set bcast=0

:loopmask
  set /a testmask=%testmask%+%testmask%
  if %decmask% EQU %testmask% (set bcast=%testmask%)
if %bcast% neq 0 (goto :goodMask)
if %testmask% geq -16777216 (goto :loopmask)

:badMask
echo.Bad subnet mask. (%NEW-MASK%)  Check and try again.
echo.
goto :End

:badIP
echo.Bad IP Address. (%NEW-IPADDR%)  Check and try again.
echo.
goto :End

:goodMask
set /a bcast="%bcast%^-1"
set /a bcast=%netAddr%+%bcast%
if %decIP% equ %bcast% (goto :badIP)

if %decIP% equ %decGW% (goto :badGW)
if %gwNetAddr% neq %netAddr% (goto :badGW)

if %decGW% equ %bcast% (goto :badGW)
if %decGW% equ %netAddr% (goto :badGW)

goto :goodGW
:badGW
echo.Bad Gateway Address. (%NEW-GW%)  Check and try again.
echo.
goto :End

:goodGW
goto %RETURN%

goto :End

:checkNum
set badNum=0
set /a numval=%num% 2> nil
if "z%num%" neq "z%numval%" (set badNum=1) else (
  if %num% GTR 255 (set badNum=1) else (
    if %num% LSS 0 (set badNum=1)
  )
)
if %badNum% equ 1 (goto %isBadLabel%)
goto :EOF

:End

Funciona mesmo no vista e 7.

---- eles esqueceram de verificar se o gateway é o endereço de rede / endereço de broadcast. ----

    
por 14.12.2011 / 15:46
0

Você pode usar FindStr com o RegEx:

/r : Uses search strings as regular expressions. Findstr interprets all metacharacters as regular expressions unless you use /l.

Using regular expressions with findstr

Findstr is capable of finding the exact text you are looking for in any ASCII file or files. However, sometimes you have only part of the information that you want to match, or you want to find a wider range of information. In such cases, findstr has the powerful capability to search for patterns of text using regular expressions.

Regular expressions are a notation for specifying patterns of text, as opposed to exact strings of characters. The notation uses literal characters and metacharacters. Every character that does not have special meaning in the regular expression syntax is a literal character and matches an occurrence of that character. For example, letters and numbers are literal characters. A metacharacter is a symbol with special meaning (an operator or delimiter) in the regular-expression syntax.

The following table lists the metacharacters that findstr accepts. enter image description here

Tenha em mente que o sistema RegEx do FindStr não é tão robusto quanto muitos outros, então nem todas as Expressões Regulares funcionarão sem uma pequena modificação.

    
por 12.12.2011 / 20:29