Thursday, May 31, 2012

Dual network interface setup

So, I have both wired and wireless available and want certain traffic to flow through specific interfaces.  The wired interface will carry the general traffic and the wireless will carry traffic only for specific private subnets.

Assume that the wireless has a gateway of 10.1.12.1 and I want all 10.0.0.0 traffic to flow thru that interface.  Also suppose I want a large portion of the 192.168.0.0 range to flow thru the same interface.  However, the IP address associated with the wireless interface will change via DHCP whenever it comes online.

The following batch file will setup a route for these two ranges to flow through the IP address associated with the wireless interface but only if the wireless gateway is a specific value.  All other traffic will flow by default through the wired interface.  Because the route tables will route the more specific traffic before the more general, I can have a local wired subnet (/24 CIDR) within either of the two 10.x.x.x/8 or 192.168.x.x/16 CIDR ranges and the general internet traffic will still flow through the wired interface.


@echo off  
 set WANGW=10.1.12.1  
 route print|find "%WANGW%">%temp%\WANIP.txt  
 for /f "tokens=4" %%i in (%temp%\WANIP.txt) do set WANIP=%%i  
 for /f "tokens=3" %%i in (%temp%\WANIP.txt) do set WANGW_FOUND=%%i  
 if %WANGW% == %WANGW_FOUND% (  
      route add 10.0.0.0/8 %WANIP%  
      route add 192.168.0.0/16 %WANIP%  
 )  
 del %temp%\WANIP.txt  
 set WANGW=  
 set WANGW_FOUND=    

The following XML file can be imported as an automated task to run the route add batch file above when the wireless adapter comes online.  Change the highlighted items as appropriate:

 <?xml version="1.0" encoding="UTF-16"?>  
 <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">  
  <RegistrationInfo>  
   <Date>2011-09-14T14:46:16.4794248</Date>  
   <Author>DOMAIN\user</Author>  
   <Description>Wireless Network Adapter connected</Description>  
  </RegistrationInfo>  
  <Triggers>  
   <EventTrigger>  
    <Enabled>true</Enabled>  
    <Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Microsoft-Windows-WLAN-AutoConfig/Operational"&gt;&lt;Select Path="Microsoft-Windows-WLAN-AutoConfig/Operational"&gt;*[System[Provider[@Name='Microsoft-Windows-WLAN-AutoConfig'] and EventID=8001]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>  
   </EventTrigger>  
  </Triggers>  
  <Principals>  
   <Principal id="Author">  
    <UserId>DOMAIN\user</UserId>  
    <LogonType>InteractiveToken</LogonType>  
    <RunLevel>HighestAvailable</RunLevel>  
   </Principal>  
  </Principals>  
  <Settings>  
   <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>  
   <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>  
   <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>  
   <AllowHardTerminate>true</AllowHardTerminate>  
   <StartWhenAvailable>false</StartWhenAvailable>  
   <RunOnlyIfNetworkAvailable>true</RunOnlyIfNetworkAvailable>  
   <IdleSettings>  
    <StopOnIdleEnd>true</StopOnIdleEnd>  
    <RestartOnIdle>false</RestartOnIdle>  
   </IdleSettings>  
   <AllowStartOnDemand>true</AllowStartOnDemand>  
   <Enabled>true</Enabled>  
   <Hidden>false</Hidden>  
   <RunOnlyIfIdle>false</RunOnlyIfIdle>  
   <WakeToRun>false</WakeToRun>  
   <ExecutionTimeLimit>P3D</ExecutionTimeLimit>  
   <Priority>7</Priority>  
  </Settings>  
  <Actions Context="Author">  
   <Exec>  
    <Command>full_path_to\routeadd_batch</Command>  
   </Exec>  
   <ShowMessage>  
    <Title>Wireless Adapter online</Title>  
    <Body>route added</Body>  
   </ShowMessage>  
  </Actions>  
 </Task>  

(code formatting courtesy of http://codeformatter.blogspot.com)