Jan 21, 2013

Modify Windows default route with a script when connecting to wifi

My corporate network proxy blocks any outgoing ssh connection, therefore in order to access my home network I would need to use the "clean" wireless connection provided by the company for special projects, test or backup scenarios.

Even if my corporate network policy does not specifically prohibit using the corporate network together with a free wireless network, the practice demonstrated that doing so would sooner or later end up with a local network outage. Since I don't have a clue why that might happen I will just take it for granted, or maybe leave you, the networking guys reading my post to provide an answer.

But how to use both corporate network and unfiltered WiFi without causing a local outage affecting around 100 hundred persons?

The answer would be deleting the default route Windows adds when connecting to the wireless network and replacing it with a specific route pointing to my IP address.
I would have loved to be able to do all this from a script which would also enable the wireless connection, but since I was not able to find an easy way to do it, I have developed another plan:

A script will be triggered whenever I use the physical switch on my laptop in order to turn wireless on and automatically connect to the predefined wireless network.

This is pretty easy to achieve in Windows 7: right click on My computer >> select Manage from the drop down list >> go to Task Scheduler  >> and select Create Task.
On the General tab, I gave the name of the task :RunWhenWirelessNetworkOn.

On the Triggers tab, select the trigger "On an Event" and choose form the Log dropdown list "Micrhosoft-Windows-NetworlProfile/Operational".


On the actions tab, select the action "Start a program" and provide the path to my script "D:\UserData\***\My Documents\bin\RouteMan.bat". I will list the content of the script a little later.


Last but not least, I don't want the script to run when I connect to each wireless network in this world, but only when I connect to the specific network whose SSID I have selected under tab Condition, from the dropdown list "Start only of the following network connection is available".


Finally hit OK to save the current settings.

Now, let's discuss the script, which should look like this:

@ECHO OFF
ECHO This script will change the wireless connection routing.
set /p runScript=Do you really want to run the script now [y/n]?:

:UINTERACTION
IF "%runScript%"=="y" GOTO ROUTEMAN
IF "%runScript%"=="n" GOTO End
set /p runScript=You need to answer with "y" or "n":
GOTO UINTERACTION


:ROUTEMAN
route delete 0.0.0.0 MASK 0.0.0.0 192.168.1.1


nslookup myaddress.dyndns.org
set /P remoteIP=Please enter remote computer address:

REM add route for myaddress.dyndns.org
route add %remoteIP% mask 255.255.255.255 192.168.1.1
tracert myaddress.dyndns.org
GOTO End


later edit for the enhanced version of the script, check the post: Enhanced script to modify the Windows default route ]



No comments:

Post a Comment