A much better way to start a VPN connection, if you use PPTP, that does not depend on the OSX GUI can be found on the "start pptp from command line" page.
So we like Apple, but sometimes their lack of attention when including crucial features is amazing. You are probably here because you just cannot seem to figure out a reliable way to automatically reconnect to a VPN server - for example, you want to maintain a connection to your University Campus but when the connection drops, you have to manually connect again.
There is such an option included which supposedly does some on-demand VPN in Snow Leopard. However, even if that option wouldn't be totally bugged, it would only solve part of the problem because the VPN would not reconnect if it disconnected - it is only tangential to what we are trying to achieve here: we want to be able to continuously monitor the connection and reconnect to the VPN as soon as the connection drops.
For that we will have to use some applescript program which will use launchctl
to be triggered in intervals and check whether the connection is up:
We create an apple script and a plist that will automatically check every 600s (10 minutes) whether the VPN is connected. If it is not connected, the apple script will attempt to reconnect. Credits go to markupboy for illustrating the connected
feature.
Using AppleScript Editor create a new script at /etc/was.vpnConnect.scpt
with the same name:
on run argv repeat with arg in argv tell application "System Events" tell current location of network preferences set VPN to service arg set vpnConnected to connected of current configuration of VPN if not vpnConnected then connect VPN end if end tell end tell end repeat end run
Save the following XML plist at /Library/LaunchDaemons/was.vpnConnect.plist
and replace NAME_OF_FIRST_VPN_SERVICE
and NAME_OF_SECOND_VPN_SERVICE
with the names of the VPN services you have configured in the network settings:
<plist version="1.0"> <dict> <key>Label</key> <string>VPN Connect</string> <key>Program</key> <string>/usr/bin/osascript</string> <key>ProgramArguments</key> <array> <string>-e</string> <string>/etc/was.vpnConnect.scpt</string> <string>NAME_OF_FIRST_VPN_SERVICE</string> <string>NAME_OF_SECOND_VPN_SERVICE</string> </array> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>600</integer> </dict> </plist>
In this case, just two VPN services are configured to automatically reconnect but you can add new items, such as:
<string>NAME_OF_THIRD_VPN_SERVICE</string> <string>NAME_OF_FOURTH_VPN_SERVICE</string>
After you have saved the plist load it from the terminal using:
launchctl load -w was.vpnConnect.plist
and let the scripts do their wizardry.
Additionally, OSX will read the plist on boot and you will not even have to log-in to start the VPN services.