Batch findstr with quotes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
raam
Posts: 3
Joined: 23 Nov 2012 13:42

Batch findstr with quotes

#1 Post by raam » 23 Nov 2012 13:45

I'm trying to figure out how to modify this script to make exact searches. I need to search for a string (eg: "123.12" -- with quotes)
I'm trying to modify a Firewall Batch Script to block certain IP's

This is the script

Code: Select all

ipconfig | findstr "Address" | qut -d: -f 2 | qni > %systemdrive%\Qaas\host_ip.txt
findstr \"%1\" %systemdrive%\Qaas\bin\host_ip.qas || findstr \"%1\" %systemdrive%\Qaas\white_list.txt || findstr \"%1\" %systemdrive%\Qaas\blocqedips.qas

IF %ERRORLEVEL% GTR 0 (
echo "NOT FOUND"
netsh ipsec stat add filter filterlist="IP List" srcaddr=%1 dstaddr=Me description=%1 protocol=any mirrored=yes
echo Blocked IP address %1, reason - %2 connections on %3 >> %systemdrive%\Qaas\Qaas-%dw%-%mm%-%dd%-%hh%.log
echo %1 >> %systemdrive%\Qaas\blocqedips.qas
) ELSE (
echo "FOUND"
echo %1 is either host IP or already blocked earlier >> %systemdrive%\Qaas\Qaas-%dw%-%mm%-%dd%-%hh%.log
)


This is the blocked IP file: http://pastebin.com/cP4BteDM

Now, trying to block, for example, 10.02.0, I'm receiving in console "10.02.02". I guess because 10.02.0 is "instr" "10.02.02". With this problem in the FINDSTR sentence, I will never be able to ban "10.02.0" for example.

Thank you in advance !!

miskox
Posts: 656
Joined: 28 Jun 2010 03:46

Re: Batch findstr with quotes

#2 Post by miskox » 23 Nov 2012 14:55

Use double quotes:

Code: Select all

findstr """10.10""" filename.txt


Hope this helps,
Saso

raam
Posts: 3
Joined: 23 Nov 2012 13:42

Re: Batch findstr with quotes

#3 Post by raam » 23 Nov 2012 15:07

Thank you for your help mate. I've tried with

Code: Select all

findstr """%1""" %systemdrive%\Qaas\blocqedips.qas


but it's still not working. Thank you anyways, I'll keep trying..

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

Re: Batch findstr with quotes

#4 Post by Squashman » 23 Nov 2012 15:24

raam wrote:Thank you for your help mate. I've tried with

Code: Select all

findstr """%1""" %systemdrive%\Qaas\blocqedips.qas


but it's still not working. Thank you anyways, I'll keep trying..

That code works based on your example.

Code: Select all

C:\Users\Squashman\Batch\findquotes>type whitelist.txt
"123.12"
"123.123"
C:\Users\Squashman\Batch\findquotes>findstr """123.12""" whitelist.txt
"123.12"

This also works.

Code: Select all

C:\Users\Squashman\Batch\findquotes>findstr /E "\"123.12\"" whitelist.txt
"123.12"

Sponge Belly
Posts: 234
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Batch findstr with quotes

#5 Post by Sponge Belly » 23 Nov 2012 15:36

Hi Raam! Welcome to DosTips.

Maybe something like this might help...

Code: Select all

findstr /bl ^"\"%~1\"^"


Remember that the dot (.) in the IP address is a findstr metacharacter and should either be escaped with a backslash (\) or turned off with findstr's /l (literal) switch.

Also, I'm guessing you want to match from the start of the IP number. That's what the /b (beginning) switch is for.

The ^" and \" combination is something I discovered by trial and error when I wanted to match a string, including surrounding quotes. Hope it works for you.

Good luck!

PS: I highly recommend reading Dave Benham's excellent article on the Undocumented Features of FindStr over at Stack Overflow.

raam
Posts: 3
Joined: 23 Nov 2012 13:42

Re: Batch findstr with quotes

#6 Post by raam » 23 Nov 2012 17:54

Thank you for your help !!!..

I know your answers are all great ! But I needed to fix this asap because I'm being ddos attacked, so I had to fix it coding a console app in C++.

I really appreciate your help.

Post Reply