IP based login script used for one subnet across 4 locations

Hello,

We are successfully using the following login script for multiple locations with different subnets.  However, we have a different situation where we support a business that uses one subnet across 4 locations.  The login script below is designed for multiple locations and subnets.  With the following login script below, we are wondering how we can change the syntax of it so we can separate a single subnet into separate groups based on where that part of the subnet is used.  We would like to be able to get a PC to map to specific servers based on a specific group area of a subnet in which it belongs to.  For using the 10.76.128.X subnet, we would want forth octet group 30-59 to map to a server A share, 60-89 to a server B share, and 90-119 to a server C share.

@ECHO OFF
IF EXIST Z: NET USE Z: /DELETE > NUL
IPCONFIG | FIND "10.76.128." > NUL
IF ERRORLEVEL 1 GOTO NotInNet1
NET USE Z: \\server1\%username% > NUL
:NotInNet1
IPCONFIG | FIND "10.76.129." > NUL
IF ERRORLEVEL 1 GOTO NotInNet2
NET USE Z: \\server2\%username% > NUL
:NotInNet2

Thanks for any help and suggestions,
Mike
miket71Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
OK, hope you are sitting comfortably.  Have try with this one, have had to adjust it a bit from when I wrote it so may need a tweak or two but give it a try:


@Echo off
set subnettocheck=128.127.1
ipconfig | find "%subnettocheck%" | find "IP Address" > %temp%\Ipaddr.txt
for /f "tokens=*" %%a in (%temp%\ipaddr.txt) do set ipaddr=%%a & goto :next
:next
 
Rem Get end of string.  replace . . . with nothing
set ipaddr=%ipaddr:. =%
 
REM Remove IP Address:
set ipaddr=%ipaddr:IP Address:=%
 
REM Remove any spaces to leave IP address x.x.x.x
set ipaddr=%ipaddr: =%
set lastoctet=%ipaddr:~-3,3%
set subnet=%ipaddr:~0,-4%
if "%lastoctet:~0,1%"=="." set lastoctet=%lastoctet:~1% & set 
 
subnet=%ipaddr:~0,-3%
if "%lastoctet:~1,1%"=="." set lastoctet=%lastoctet:~2% & set 
 
subnet=%ipaddr:~0,-2%
 
echo Your IP address is %ipaddr%
echo Your subnet is %subnet%
echo Last octet is %lastoctet%
 
if %lastoctet% LSS 30 echo Less than 30 & goto subnet30
if %lastoctet% LSS 60 echo Less than 60 & goto subnet60
if %lastoctet% LSS 90 echo Less than 90 & goto subnet90
if %lastoctet% LSS 220 echo Less than 220 & goto subnet220
 
goto end
:subnet30
 
goto end
:subnet60
 
goto end
:subnet90
 
goto end
:subnet220
 
:end

Open in new window

0
SteveGTRCommented:
Here's my version:
@echo off
 
setlocal
 
set subnet=10.76.128.
 
for /f "tokens=2 delims=:" %%a in ('ipconfig^|findstr /i /c:"%subnet%"') do (
  for /f "tokens=4 delims=." %%b in ('echo %%a') do set hostid=%%b&goto FOUND
)
 
echo Could not find host ID
goto :EOF
 
:FOUND
 
echo hostid=%hostid%
 
if /i %hostid% LEQ 59 (
  if /i %hostid% GEQ 30 echo 30-59 range
  goto :EOF
)
 
if /i %hostid% LEQ 89 echo 60-89 range&goto :EOF
if /i %hostid% LEQ 119 echo 90-119 range&goto :EOF

Open in new window

0
Steve KnightIT ConsultancyCommented:
Two lines there wrapped oddly in code window.  Trying it straight in here.   The subnettocheck variable you need to set to the first three octets to check for so that it will exclude any RAS, VPN or other IP addresses assigned and just look for the 'right' one.

Actually thinking differently here, do they use a 255.255.255.0 subnet mask or is it smaller one, i.e. are there different routers at each site?  if so you could ipconfig | find "ateway" | find "x.x.x.x" to check which site they are on.

hth

Steve

@Echo off
set subnettocheck=128.127.1
ipconfig | find "%subnettocheck%" | find "IP Address" > %temp%\Ipaddr.txt
for /f "tokens=*" %%a in (%temp%\ipaddr.txt) do set ipaddr=%%a & goto :next
:next
 
Rem Get end of string.  replace . . . with nothing
set ipaddr=%ipaddr:. =%
 
REM Remove IP Address:
set ipaddr=%ipaddr:IP Address:=%
 
REM Remove any spaces to leave IP address x.x.x.x
set ipaddr=%ipaddr: =%
set lastoctet=%ipaddr:~-3,3%
set subnet=%ipaddr:~0,-4%
if "%lastoctet:~0,1%"=="." set lastoctet=%lastoctet:~1% & set subnet=%ipaddr:~0,-3%
if "%lastoctet:~1,1%"=="." set lastoctet=%lastoctet:~2% & set subnet=%ipaddr:~0,-2%
 
echo Your IP address is %ipaddr%
echo Your subnet is %subnet%
echo Last octet is %lastoctet%
 
if %lastoctet% LSS 30 echo Less than 30 & goto subnet30
if %lastoctet% LSS 60 echo Less than 60 & goto subnet60
if %lastoctet% LSS 90 echo Less than 90 & goto subnet90
if %lastoctet% LSS 220 echo Less than 220 & goto subnet220
 
goto end
:subnet30
 
goto end
:subnet60
 
goto end
:subnet90
 
goto end
:subnet220
 
:end
 
Open in New Window
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

miket71Author Commented:
Dragon-it,

They use a 255.255.255.224 subnet mask at all locations.  Also, at which point in these scripts would the drive mapping net use commands come into play?  I was thinking at the end of what you have posted, but I wanted to make sure.

Stevegtr, thank you as well for your suggestion.  We'll give it a try and let you know.

Thanks,
Mike
0
Steve KnightIT ConsultancyCommented:
You would put the net use commands in the subroutines between subnet30 and goto end etc.
e.g.

:subnet30
net use \\serverforsubnet0to30\share
goto end
:subnet60
 net use \\serverforsubnet30to89\share
goto end
:subnet90
 
goto end
:subnet220
 
:end

As they use a subnet mask of 255.255.255.224 then the easiest bet is to search for the default gateways then as they will all be different and easily identify which subnet the person is on, i.e. similar to you original batch:


@ECHO OFF
IF EXIST Z: NET USE Z: /DELETE > NUL
IPCONFIG | FIND "ateway" | find "10.76.128.31" > NUL
IF ERRORLEVEL 1 GOTO NotInNet1
NET USE Z: \\server1\%username% > NUL
:NotInNet1
IPCONFIG | find "ateway" | FIND "10.76.128.63" > NUL
IF ERRORLEVEL 1 GOTO NotInNet2
NET USE Z: \\server2\%username% > NUL
:NotInNet2

which means search for the "ateway" to get the default gateway address and then search for the IP address of the gateway for that site.... too late and tired to work out the correct gateways from that subnet mask at the moment but easy enough for you find out.
0
oBdACommented:
Why not simply do it the official way, by calculating the subnet from the IP address and the subnet mask?
The code below will return the real subnet and save it in the variable LocalNet. Based on this variable, you can then "goto" different labels as usual.

@echo off
setlocal
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "IP Address"') do set IP=%%a
set IP=%IP: =%
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "Subnet"') do set Mask=%%a
set Mask=%Mask: =%
for /f "tokens=1-4 delims=." %%a in ("%IP%") do (
  set s1=%%a
  set s2=%%b
  set s3=%%c
  set s4=%%d
)
for /f "tokens=1-4 delims=." %%a in ("%Mask%") do (
  set /a s1 "&=" %%a
  set /a s2 "&=" %%b
  set /a s3 "&=" %%c
  set /a s4 "&=" %%d
)
set LocalNet=%s1%.%s2%.%s3%.%s4%
echo Local Subnet: %LocalNet%

Open in new window

0
SteveGTRCommented:
You could do that, but it doesn't work on Vista. I like it though :)
0
oBdACommented:
Don't have Vista at hand at the moment, but this should now work on Vista as well:

@echo off
setlocal
set IPAddressString=IP Address
ver | find /i "Version 6" && set IPAddressString=IPv4 Address
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "%IPAddressString%"') do set IP=%%a
set IP=%IP: =%
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "Subnet"') do set Mask=%%a
set Mask=%Mask: =%
for /f "tokens=1-4 delims=." %%a in ("%IP%") do (
  set s1=%%a
  set s2=%%b
  set s3=%%c
  set s4=%%d
)
for /f "tokens=1-4 delims=." %%a in ("%Mask%") do (
  set /a s1 "&=" %%a
  set /a s2 "&=" %%b
  set /a s3 "&=" %%c
  set /a s4 "&=" %%d
)
set LocalNet=%s1%.%s2%.%s3%.%s4%
echo Local Subnet: %LocalNet%

Open in new window

0
SteveGTRCommented:
That worked well.
0
Steve KnightIT ConsultancyCommented:
Of course a Windows way of doing this would perhaps be to create a domain DFS root with links to each local file server then connecting the user to \\domainname.local\share would always hit the local subnet server anyway.... in theory.
0
Steve KnightIT ConsultancyCommented:
Thanks for the points / answer there.  I guess you used one of my suggestions in the end then, though you had a good few different versions there to try.

thanks

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.