Using IF to Compare IP Addresses?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Using IF to Compare IP Addresses?

#1 Post by Samir » 30 Aug 2014 10:42

I'm using the following to compare IP addresses and it's not giving me the expected output. What could be wrong?

Code: Select all

@echo off
set ip_address_string="IP Address"
rem Uncomment the following line when using Windows 7 or Windows 8 / 8.1 (with removing "rem")!
rem set ip_address_string="IPv4 Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
echo%%f
IF NOT "%%f"=="192.168.5.8" ECHO NOT SAME!
goto :eof
)

Any assistance appreciated. 8)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using IF to Compare IP Addresses?

#2 Post by foxidrive » 30 Aug 2014 10:45

Use this and see what it shows. You may see extra spaces etc.

Code: Select all

echo "%%f"

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Using IF to Compare IP Addresses?

#3 Post by Samir » 30 Aug 2014 11:35

foxidrive wrote:Use this and see what it shows. You may see extra spaces etc.

Code: Select all

echo "%%f"
I'm suspecting there's something like that in there. But the results to your change are a bit odd:

Code: Select all

" 192.168.5.8
NOT SAME!
I even tried using ' with the same results. :( Thoughts?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Using IF to Compare IP Addresses?

#4 Post by Samir » 30 Aug 2014 11:45

So I did some more experimenting based on the %%nxf string trim method we discussed here a while back. And there's some odd results. :shock:

Changing %%f to %%~nxf did nothing! :shock:

Changing %%f to %%~nf resulted in an even more interesting result:

Code: Select all

" 192.168.5"
NOT SAME!
:shock:

What's going on? :?:

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Using IF to Compare IP Addresses?

#5 Post by Samir » 30 Aug 2014 11:51

Solved! (Always understand pieces of code you're borrowing from other places. :oops: )

Looks like the usebackq was causing issues when I was trying to do a comparison inside the for loop. Once I wrote %%f to a variable and then compared with the variable outside the for loop, all works as intended. 8)

Working version:

Code: Select all

@echo off
set ip_address_string="IP Address"
rem Uncomment the following line when using Windows 7 or Windows 8 / 8.1 (with removing "rem")!
rem set ip_address_string="IPv4 Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
set IP=%%f
ECHO%%f
goto :eof
)
IF NOT !IP!=="192.168.5.8" ECHO NOT SAME!


Now if I could only make it faster. I'm sure cleaning it up a bit wouldn't hurt either. Any suggestions?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Using IF to Compare IP Addresses?

#6 Post by Samir » 30 Aug 2014 12:19

Well, I still have it messed up somewhere because that IF still isn't comparing right...

Fixed it! It was the goto eof. :oops:

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Using IF to Compare IP Addresses?

#7 Post by Compo » 30 Aug 2014 12:21

It's got nothing to do with the UseBackQ, your problem is that you are using the colon (:) as the delimiter. That is always followed by a space (as shown in your quoted output example).

All you need to do is make the comparison by including a space in your match or remove the space first.

Code: Select all

@Echo Off & SetLocal EnableExtensions
Set SrchStr=192.168.5.8
Set IPStr="IPv4 Address"
(Set AddStr=)
For /f "tokens=1-2 delims=." %%A In ('Ver') Do Set OSVer=%%A.%%B
If %OSVer:~-2,1% Equ . (If %OSVer:~-3% Lss 6.1 Set IPStr=%IPStr:v4=%)
For /f "tokens=*" %%A In ('IPConfig^|Find /i %IPStr%') Do (
   Set AddStr=%%A)
If Not Defined AddStr GoTo :EOF
If %AddStr:*: =% NEq %SrchStr% Echo( NOT SAME!
Pause >Nul
I really see no reason to attempt to speed up your script, how much time is it taking?

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

Re: Using IF to Compare IP Addresses?

#8 Post by Squashman » 30 Aug 2014 13:41

Not sure what you were expecting for output using the command modifiers. They did exactly what they are suppose to do.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Using IF to Compare IP Addresses?

#9 Post by Samir » 30 Aug 2014 15:59

Compo wrote:It's got nothing to do with the UseBackQ, your problem is that you are using the colon (:) as the delimiter. That is always followed by a space (as shown in your quoted output example).

All you need to do is make the comparison by including a space in your match or remove the space first.

Code: Select all

@Echo Off & SetLocal EnableExtensions
Set SrchStr=192.168.5.8
Set IPStr="IPv4 Address"
(Set AddStr=)
For /f "tokens=1-2 delims=." %%A In ('Ver') Do Set OSVer=%%A.%%B
If %OSVer:~-2,1% Equ . (If %OSVer:~-3% Lss 6.1 Set IPStr=%IPStr:v4=%)
For /f "tokens=*" %%A In ('IPConfig^|Find /i %IPStr%') Do (
   Set AddStr=%%A)
If Not Defined AddStr GoTo :EOF
If %AddStr:*: =% NEq %SrchStr% Echo( NOT SAME!
Pause >Nul
I really see no reason to attempt to speed up your script, how much time is it taking?
I tried putting a space and that didn't fix it either.

As far as speed, it isn't instant (which is what I usually like about batch), so it's taking a second or so.

I actually scrapped trying to find the IP address and match it that way. Much easier to just do a find to see if what I don't want is the IP address. Then I just use the errorlevel to branch.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Using IF to Compare IP Addresses?

#10 Post by Samir » 30 Aug 2014 16:19

Squashman wrote:Not sure what you were expecting for output using the command modifiers. They did exactly what they are suppose to do.
It was someone else's code, so I didn't really check all the ins and outs of exactly what it was doing. I probably should have looked at the logic and coded a version myself.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using IF to Compare IP Addresses?

#11 Post by foxidrive » 30 Aug 2014 20:59

Samir, You seem to be using XP as XP has a flaw in the output of ipconfig which causes the behaviour you saw (missing trailing quote in the %%f)

You can see here in a hex view of the output using this command
ipconfig >file.txt
where the characters at the end of the line are not the normal 0D 0A
0D 0A hex are equivalent to 13 10 in decimal, and ascii 13 is a carriage return while ascii 10 is a line feed.


This is Windows XP output:

Code: Select all

0B40:0100  0D 0D 0A 57 69 6E 64 6F-77 73 20 49 50 20 43 6F   ...Windows IP Co
0B40:0110  6E 66 69 67 75 72 61 74-69 6F 6E 0D 0D 0A 0D 0D   nfiguration.....
0B40:0120  0A 0D 0D 0A 45 74 68 65-72 6E 65 74 20 61 64 61   ....Ethernet ada
0B40:0130  70 74 65 72 20 4C 6F 63-61 6C 20 41 72 65 61 20   pter Local Area
0B40:0140  43 6F 6E 6E 65 63 74 69-6F 6E 20 31 34 3A 0D 0D   Connection 14:..
0B40:0150  0A 0D 0D 0A 20 20 20 20-20 20 20 20 43 6F 6E 6E   ....        Conn
0B40:0160  65 63 74 69 6F 6E 2D 73-70 65 63 69 66 69 63 20   ection-specific
0B40:0170  44 4E 53 20 53 75 66 66-69 78 20 20 2E 20 3A 20   DNS Suffix  . :


This is Windows 8 output:

Code: Select all

0B44:0100  0D 0A 57 69 6E 64 6F 77-73 20 49 50 20 43 6F 6E   ..Windows IP Con
0B44:0110  66 69 67 75 72 61 74 69-6F 6E 0D 0A 0D 0A 0D 0A   figuration......
0B44:0120  45 74 68 65 72 6E 65 74-20 61 64 61 70 74 65 72   Ethernet adapter
0B44:0130  20 4C 6F 63 61 6C 20 41-72 65 61 20 43 6F 6E 6E    Local Area Conn
0B44:0140  65 63 74 69 6F 6E 20 32-3A 0D 0A 0D 0A 20 20 20   ection 2:....
0B44:0150  43 6F 6E 6E 65 63 74 69-6F 6E 2D 73 70 65 63 69   Connection-speci
0B44:0160  66 69 63 20 44 4E 53 20-53 75 66 66 69 78 20 20   fic DNS Suffix
0B44:0170  2E 20 3A 20 0D 0A 20 20-20 4C 69 6E 6B 2D 6C 6F   . : ..   Link-lo



When using XP and ipconfig you see 0D 0D 0A where there are two carriage returns in sequence at the end of each line.
This is fixed in Windows Vista, 7 and 8 but there is a workaround for XP

The second issue you had was explained by compo - when you use a single delimiter of : then you get the leading space - which I was trying to let you discover, but it was complicated by the above.

Lastly - when using %%~nA it gives you the filename part of the string, but an IP address has an "extension" which cmd recognises as the part from the last period in the string. That is why the last octet of the IP address wasn't printed.
You can use %%~nxA when parsing strings to make sure that it never removes the end of the string by mistake.

I hope that helps to explain some of the odd behaviour that you saw.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Using IF to Compare IP Addresses?

#12 Post by Samir » 01 Sep 2014 17:53

Thank you for the wonderfully detailed explanation foxidrive!

I felt like there might have been a bug with the cr, but threw that out as a possibility as that would mean there was a permanent bug in xp. :oops:

Eventually I went with a different approach as I was trying to extract the IP address to do a comparison test to see if the subnet was not what I wanted. After doing some searches, I found that an easier method was to use find and errorcheck that to get the result I needed. 8)

Nevertheless, I learned a lot from this exercise and appreciate all the feedback!

Post Reply