Last Updated: February 25, 2016
·
2.281K
· christianromney

Automatically add routes when connecting to a VPN

When connecting to your company's VPN you may find yourself needing to add routes to your DMZ servers. You can automate this process by installing an new launchctl (like cron for OS X) that runs a script like the following every minute (your routes and interface name may vary).

#!/bin/bash
connected=$(ifconfig | grep ppp0)
if [ -n "$connected" ]; then
  added=$(route get -net 192.168.200 | grep ppp0)
  if [ -z "$added" ]; then
    logger "VPN connection detected, adding routes"
    sudo route -n add -net 192.168.200 -interface ppp0
    sudo route -n add -net 192.168.170 -interface ppp0
  fi
fi