How do I automatically connect to a VPN when using Wi-Fi in OS X? [duplicate]

Solution 1:

I created a script and launch agent that will automatically connect to a VPN connection when it cannot ping a specific address. It could probably be modified in a way that looks at wifi network whitelist and connects to the VPN if the current wifi is not in that list. As it is right now the vpn will always try to connect if it cannot hit an address that is on the vpn network.

The agent calls a shell script every 30 seconds and tries to ping an IP address that is static on the vpn network. If it cannot ping that IP it enables your vpn connection.

If you did this through an Apple Script App, the app icon will always be in your dock. I prefer to have this running automatically in the background.

Clone the project below and follow the directions in the readme. The end result is an installer package that will place a launch agent plist file in /Library/LaunchAgents/ and a shell script in /Library/Application Support/melonsmasher/.

Be sure to edit the shell script(auto-vpn) with your VPN connection name and an IP address that is on the VPN network. You can change the run interval in the plist file(com.melonsmasher.autovpn.plist).

https://github.com/MelonSmasher/OSX-AutoVPN