Regular expressions not working in batch script for pinging subnet

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
synthesis
Posts: 1
Joined: 18 Sep 2016 04:31

Regular expressions not working in batch script for pinging subnet

#1 Post by synthesis » 06 Oct 2016 10:49

Hello. I have the following batch script to ping all ip addresses on my subnet:

FOR /L %%i IN (1,1,254) DO PING -n 1 192.168.2.%%i | FINDSTR /i /R "Reply from ^(192.168.4.20)"

I want to only show machines which reply to the ping, excluding a "Reply from 192.168.4.20: Destination host unreachable" (192.168.4.20 is the machine which is doing the pinging)
I have used ^(192.168.4.20) to exclude this line. However, the above line hangs when run and doesn't work, despite using the /R regular expression switch to exclude any lines which contain "Reply from 192.168.4.20".
What could be the problem?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Regular expressions not working in batch script for pinging subnet

#2 Post by Squashman » 06 Oct 2016 10:56

I am not understanding why you think you need to use a Regular Expression or parentheses? That is definitely not the code you would use to exclude a match.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Regular expressions not working in batch script for pinging subnet

#3 Post by dbenham » 06 Oct 2016 11:42

There are a number of reasons why your code is not working as you expect:

1) FINDSTR treats a search like "A B" as a search for A or B (two different searches, space delimited). If you want a search to contain space literals, then the /C:"search with spaces" option must be used.

2) FINDSTR has extremely limited regular expression support. Among other things, it does not support grouping by parentheses.

3) Even if you substituted some other full featured regular expression search tool (like grep for Windows), your regex is completely wrong. You need to study the syntax of regular expressions more. If you want to match a dot literal, then it must be escaped. And the ^ is an anchor meaning the next expression must begin a line. You would need a negative look-ahead assertion instead. Something like (?!192\.168\.4\.20)"

4) I'm not an expert on PING, so maybe I'm missing something. But why would PING 192.168.2.n ever return 192.168.4.n? Shouldn't both colored numbers be the same (either 2 or 4)?

Assuming you intended to use 4 everywhere, then you should filter out the number before you PING. That way your FIND or FINDSTR filter need not worry about the address.

Code: Select all

FOR /L %%i IN (1,1,254) DO IF %%i NEQ 20 PING -n 1 192.168.4.%%i | FIND "Reply from "


Dave Benham

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Regular expressions not working in batch script for pinging subnet

#4 Post by trebor68 » 07 Oct 2016 05:22

The search string (RegEx), which can be used here, is unfortunately not possible to enter in FINDSTR.
Search string: ^Reply from 192\.168\.4\.(?!20:)\d+

A solution with FINDSTR is possible.

The blanks can be replaced with a dot.
The dots between the digits are replaced by a metacharacter.
Each search area is defined separately.

Here is the search for FINDSTR:

Code: Select all

for /l %%a in (1, 1, 254) do (echo Reply from 192.168.4.%%a:) | findstr /r "^Reply.from.192\.168\.4\.[123456789]: ^Reply.from.192\.168\.4\.[13456789][0123456789]: ^Reply.from.192\.168\.4\.[2][123456789]: ^Reply.from.192\.168\.4\.[12][0123456789][0123456789]:"

To understand:

Code: Select all

For any adress: 192.168.4.xxx

xxx with numbers from 1 to 9
"^Reply.from.192\.168\.4\.[123456789][:]"

xxx with numbers from 10 to 19 and from 30 to 99
"^Reply.from.192\.168\.4\.[13456789][0123456789][:]"

xxx with numbers from 21 to 29
"^Reply.from.192\.168\.4\.[2][123456789][:]"

xxx with numbers from 100 to 299; Numbers higher as 254 not using by the FOR command.
"^Reply.from.192\.168\.4\.[12][0123456789][0123456789][:]"

The solution from dbenham is shorter.

Post Reply