Page 1 of 1

Batch findstr with quotes

Posted: 23 Nov 2012 13:45
by raam
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 !!

Re: Batch findstr with quotes

Posted: 23 Nov 2012 14:55
by miskox
Use double quotes:

Code: Select all

findstr """10.10""" filename.txt


Hope this helps,
Saso

Re: Batch findstr with quotes

Posted: 23 Nov 2012 15:07
by raam
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..

Re: Batch findstr with quotes

Posted: 23 Nov 2012 15:24
by Squashman
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"

Re: Batch findstr with quotes

Posted: 23 Nov 2012 15:36
by Sponge Belly
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.

Re: Batch findstr with quotes

Posted: 23 Nov 2012 17:54
by raam
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.