Solved IP Address prefix extracton

Dell Inspiron 11z notebook - customizabl...
May 27, 2011 at 18:02:04
Specs: Windows XP
I'm trying to return the first two octet of an ip address for comparison but it does not work for both locations. For instance, I'm in loc. A and my ip starts with 10.38.xx.xx, so the script indicates that I'm in loc. A. However, if I run this from loc. B whose IP begins with 10.9.xx.xx, it does not work because the prefix is always 10.9. (with extra dot) My question, is there anyway to make the script return only the first 2 octet of the ip address? i.e. if i ran the script from loc A or B, it should only return 2 octets i.e. xx.xx and nothing more. Also, when executed from loc B, it returns "your location cannot be determined" which is under the 3rd label in addition to returning "You are in loc. B".

TIA.

@echo off & setLocal EnableDELAYedeXpansion

set "loca=10.38"

set "locb=10.9"

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "IP"') do (
set IP=%%a
echo.Your IP Address is:!IP!

)
set prefix=!IP:~1,4!
echo.!prefix!
call isA
goto:eof

:isA
if not !IP!==!loca! call isB
echo.&echo.Your location is A
goto:eof


:isB
if not !IP!==!==!locb! call isOther
echo.&echo.Your location is B
goto:eof

:isOther
echo.&echo.Your location cannot be determined.


See More: IP Address prefix extracton

Report •

#1
May 27, 2011 at 23:13:31
Hi..below is part of your sciript.

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "IP"') do (
set IP=%%a
echo.Your IP Address is:!IP!

)


this script gives you the ip like 10.10.10.10

do another for loop with . as a delimiter and take 2nd token (it will give you exact 2 or 38) and in your set condition instead of using 10.9=locA use 9=Loc A.

If u want u can include both deliminators in same loop..but go for easy way first and when working u can improve it more...

Try it first urself ...if stuck at somewhere feel free to post back...

Subhash Chandra.
http://www.wintechgeek.com


Report •

#2
May 28, 2011 at 14:20:49
No luck. Could you post a sample code of what you are saying? Thanks.

Report •

#3
May 29, 2011 at 13:44:09
✔ Best Answer
try this ..
===========================================================
@echo off & setlocal enabledelayedexpansion & set loc=


Set locA=38
Set locB=9

for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "IPv4 Address"') do (
set IP=%%a
for /f "tokens=2 delims=." %%i in ("!IP!") do (
Set loc=C
IF /i %%i == %locA% set loc=A
If /i %%i == %locB% set loc=B


Echo.As per the Second octoate [ %%i ] your location is identified as Location !loc!
)
)
======================================================

It looks for the 2nd octoate of the IP address (as per your requirements). If its 38 then display location A , If 9 then display LOC B otherwise for everything else location C.

If you need to modify the IP seetings.. change it in this line

Set locA=38
Set locB=9

Subhash Chandra.
http://www.wintechgeek.com


Report •

Related Solutions

#4
May 29, 2011 at 15:13:57
Thank you. It works great. Very much appreciated.

Report •

#5
May 29, 2011 at 21:54:16
you welcome..Glad you came back to post feedback :)

Subhash Chandra.
http://www.wintechgeek.com


Report •

Ask Question