1.

Solve : How to read an IP Address in a batch file?

Answer»

Hi

I connect to a VPN when working out of the office. I often want to only send my MS Exchange information down the VPN though and use the local internet CONNECTION for everything ELSE. I do this by adding a route via batch file, as below

@echo off
@route delete 0.0.0.0
@route -p add 0.0.0.0 mask 0.0.0.0 192.168.2.3
@route add 192.168.10.1 mask 255.255.255.255 192.168.10.188
@exit

The trouble is that as I get a different IP Address from the VPN everytime I connect I was wondering if there was a way in a batch file to read an IP address from a particular adapter (my VPN connection) so I don't have to manually edit the batchfile everytime I connect to change the local IP address (192.168.10.188 in my example). Any help would be appreciatedIf the IP address is returned from ipconfig /all, you could pipe the information to the find command and extract it that way.

Good luck. Try this, it will only work if the VPN connection is the last entry when using ipconfig /all. Hope this helps.

:: Note: the following code will extract only the last IP address from the list
ECHO.
FOR /F "TOKENS=2* DELIMS=:" %%A IN ('IPCONFIG /ALL ^| FIND "IP Address"') DO FOR %%B IN (%%A) DO SET IPADDR=%%B


:: RECORDING the IP Address to a text file and adding the routes for the session.
ECHO %IPADDR% >%temp%.\ipaddress.txt
route add 1.1.0.0 mask 255.255.0.0 %IPADDR%
route add 2.2.0.0 mask 255.255.0.0 %IPADDR%


:: Find server then add a specific route, this only works after adding routes to name server you are using.

FOR /F "TOKENS=2* DELIMS=:" %%A IN ('nslookup server.domain.com 1.1.?.? ^| FIND "Address:"') DO FOR %%B IN (%%A) DO SET FOUND=%%B
ECHO %FOUND% >%temp%.\FOUND.txt
route add %FOUND% mask 255.255.255.255 %IPADDR%Thank you very much noisy cricket, that WORKED fantastically. A great time saver if anyone else has these kind of VPN issues! I am glad it helped. I also have a batch file that will clenup the routes when you want to disconnect. If you would LIKE that script send me an e-mail at [emailprotected]



Discussion

No Comment Found