How to selectively route network traffic through VPN on Mac OS X Leopard?
Create the file /etc/ppp/ip-up with following content:
#!/bin/sh
/sbin/route add <SUBNET> -interface $1
replacing <SUBNET>
with subnet, you want to route through VPN (for ex. 192.168.0.0/16)
execute as root:
chmod 0755 /etc/ppp/ip-up
This file will be executed each time you connect to VPN.
The parameters given to the script:
$1
: The VPN interface (e.g.ppp0
)$2
: Unknown, was0
in my case$3
: IP of the VPN server$4
: VPN gateway address$5
: Regular (non-vpn) gateway for your lan connections
There is a hidden feature in Network Preferences on MacOS: you can sort interfaces.
Open System Preferences -> Network -> Click the gear
bottom left -> Set service Order...
It's critical that you have your network interfaces sorted into the order you want them to be used. If you want ALL non-LAN data to go to the VPN, put the VPN interface at the top. Sort like this
- VPN
- Ethernet
- Airport
Not like this:
- Airport
- Ethernet
- VPN
This way, no need to check the following setting in Session Options
:
Send all traffic over VPN connection
✅ Tested on L2TP VPN
connection
I wanted to do a similar thing. Connect the VPN and then route an additional network via that VPN. I ended up with the following bit of Applescript:
-- Connect Work VPN
tell application "System Events"
tell network preferences
tell current location
tell service "Work"
connect
tell current configuration
repeat until get connected = true
delay 1
end repeat
end tell
end tell
end tell
end tell
end tell
set gateway to "192.168.1.1"
do shell script "route add 172.16.0.0/16 " & gateway with administrator privileges
You need to change "Work"
to the name of your VPN connection, 192.168.1.1
to your gateway address, and 172.16.0.0/16
to the address of the network to which you wish to route. Additional networks can be added by repeating the final line with different addresses.