-1

I'm trying to validate if the IP address argument passed to a batch file is valid or not. Unfortunately, Windows findstr is not reliably working though the regex fed into is matched.

Following is the summary of all the results.

findstr sets errorlevel to 0 if there is a match, 1 if there is no match.

C:\iSTEP\VMTicketing\RealVNC>echo 192.10.10.10 | findstr /R "[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]"

C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1

C:\iSTEP\VMTicketing\RealVNC>echo 192.101.101.101 | findstr /R "[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]"
192.101.101.101

C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
0

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /R "[0-2][0-9][0-9]?\.[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?"

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /R "[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?\.[0-2]?[0-9][0-9]?"

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"

C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}*"
FINDSTR: No search strings

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
FINDSTR: No search strings

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}*"

C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1

C:\iSTEP\VMTicketing\RealVNC>echo 1.1.1.1 | findstr /b /e /R "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\>"

C:\iSTEP\VMTicketing\RealVNC>echo %errorlevel%
1

Commandoutput

Manohar M
  • 3
  • 2
  • For info on findstr syntax and limitations, [Please take a look here](https://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman) – T3RR0R Sep 03 '21 at 04:45
  • Thank you, but, this doesnt list anything specific to the digit matching limitation, or did I overlook something? – Manohar M Sep 03 '21 at 05:05
  • You've missed something. findstr does not support the regex pattern you are attempting to use. – T3RR0R Sep 03 '21 at 05:07
  • @T3RR0R With the findstr, we've /r which supports regex and with this any general regex should be supported (atleast basic digit match) or? – Manohar M Sep 03 '21 at 05:35
  • findstr does not support the regex syntax common to other languages. This fact and the details of the supported syntax are explaind at the link I posted previously. – T3RR0R Sep 03 '21 at 05:43
  • Tricky way is ping-utility usage to check for real name or IP: `ping -n 1 1.1.1.1 -w 1 | (findstr "could not find host" && @echo 1 )||echo 0` – Daemon-5 Sep 03 '21 at 05:55
  • Thanks for the hint with powershell, initial tests are promising, – Manohar M Sep 03 '21 at 06:05
  • Type `findstr /?` into a Command Prompt window and learn about the supported search expressions… – aschipfl Sep 03 '21 at 08:51

2 Answers2

0

A simple way of performing matches using powershell regex:

@Echo off
 Call :Regex "131.21.1.101" "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
 Echo(%Errorlevel%
 Call :Regex "1321.101" "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
 Echo(%Errorlevel%
Goto :Eof

:Regex
 powershell.exe -noprofile -c "'%~1' -match '%~2'" | findstr /l "True" > nul || Exit /b 1
Exit /b 0

For details on powershell regex syntax see here

T3RR0R
  • 2,747
  • 3
  • 10
  • 25
0

I just tried the following:

echo 1.1.1.1 | findstr "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"

It looks like it's working fine, but there are other issues, like a weird value like isolationlayer_31bf3856ad364e35_10.0.14393.479_none_3b8c also being shown.

Dominique
  • 16,450
  • 15
  • 56
  • 112