9

after I make a VPN connection to my work/whatever, I currently have to go into the command prompt and manually add a route.

eg.

ROUTE ADD 10.1.0.0 255.255.0.0 172.16.3.0 METRIC 1 or whatever the command is.

Is it possible to have this automatically happen after I successfully make a VPN connection?

Pure.Krome
  • 6,698

4 Answers4

24

If you have multiple VPNs you might run into the issue that when they connect in random order, their interface IDs change. In that case the normal ROUTE -P ADD 10.0.0.0 MASK 255.255.0.0 10.0.0.1 IF 42 does not work. The next time the VPN connects it might have a different interface number.

Powershell has a cmdlet available that adds routes on VPN connection and removes them again when the VPN is disconnected: Add-VpnConnectionRoute. It works without having to specify the interface ID.

The basic syntax is like this:

Add-VpnConnectionRoute -ConnectionName "VPN Connection Name" -DestinationPrefix 10.0.0.0/16

After entering this command, the routes will be created/removed automatically on connection/disconnection of the VPN.

ErikvO
  • 389
5

If you want to make it a 1-step process, you could create a batch file that runs rasdial to automate your VPN connection and then does a ROUTE ADD:

rasdial "connection name" username password ('*' to prompt for password)
ROUTE ADD 10.1.0.0 255.255.0.0 172.16.3.0 

This assumes you're connecting to a Microsoft VPN, but you could script the OpenVPN client in the same way:

openvpn c:\path\to\config-file.ovpn
ROUTE ADD 10.1.0.0 255.255.0.0 172.16.3.0 
nedm
  • 5,710
4
netsh interface ipv4 add route [destination/prefixlength] "[interface/connection name]"

I'm using that to deal with connections that have subnet overlap by adding static routes for hosts on the remote subnet - servers and the like.

1

You could make the route persistent (I think with route -p) so you don't need to enter it each time. If you are using openVPN, the server can send a route to the client: push "route 192.168.1.0 255.255.255.0" for example. With other VPNs servers I dont' know but I guess they may have a similar option too.

laurent
  • 2,065