Obtain IP Address (and parse Octets) and Gateway from DOS
Posted: 12 Jan 2010 20:15
This is really useful for running domain login scripts based on IP Subnet.
Six months ago when I was trying to figure out how to do this, I couldnt. Most results on google returned the use of a third party application that needed to be installed on every workstation in the domain in order to process the loginscript correctly. I ended up utilizing the %logonserver% variable to select the logonscript because the hostnames of our domain controllers are prefixed with their location. But this wouldn't work for everyone. With what I've learned in the past week since finding this site, I was able to write the following code which solved ALL my problems, I implimented it last night in my domains logonscript and it's working perfectly.
REM.-- Beginning of application --
route print 0.0.0.0 |findstr 0.0.0.0 > tmp.txt
for /f "tokens=3,4" %%a in (tmp.txt) do (
set gateway=%%a
set ipaddress=%%b
)
del tmp.txt
for /f "tokens=1-4 delims=." %%i in ("%IPADDRESS%") do (
set ipoct1=%%i
set ipoct2=%%j
set ipoct3=%%k
set ipoct4=%%l
)
IF "%ipoct3%"=="10" cscript "%LOGONSERVER%\NETLOGON\MapPrinters_VA.vbs"
IF "%ipoct3%"=="11" cscript "%LOGONSERVER%\NETLOGON\MapPrinters_IL.vbs"
IF "%ipoct3%"=="12" cscript "%LOGONSERVER%\NETLOGON\MapPrinters_MO.vbs"
:EOF
... Enjoy! Hope someone finds this useful.
Six months ago when I was trying to figure out how to do this, I couldnt. Most results on google returned the use of a third party application that needed to be installed on every workstation in the domain in order to process the loginscript correctly. I ended up utilizing the %logonserver% variable to select the logonscript because the hostnames of our domain controllers are prefixed with their location. But this wouldn't work for everyone. With what I've learned in the past week since finding this site, I was able to write the following code which solved ALL my problems, I implimented it last night in my domains logonscript and it's working perfectly.
REM.-- Beginning of application --
route print 0.0.0.0 |findstr 0.0.0.0 > tmp.txt
for /f "tokens=3,4" %%a in (tmp.txt) do (
set gateway=%%a
set ipaddress=%%b
)
del tmp.txt
for /f "tokens=1-4 delims=." %%i in ("%IPADDRESS%") do (
set ipoct1=%%i
set ipoct2=%%j
set ipoct3=%%k
set ipoct4=%%l
)
IF "%ipoct3%"=="10" cscript "%LOGONSERVER%\NETLOGON\MapPrinters_VA.vbs"
IF "%ipoct3%"=="11" cscript "%LOGONSERVER%\NETLOGON\MapPrinters_IL.vbs"
IF "%ipoct3%"=="12" cscript "%LOGONSERVER%\NETLOGON\MapPrinters_MO.vbs"
:EOF
... Enjoy! Hope someone finds this useful.