I made some changes to the provided answer, because if something is worth doing it's worth doing into the ground. I wanted to reconnect if the VPN was dropped, but NOT reconnect if the VPN was intentionally disconnected. The solution I came up with was both effective and inelegant.
First I added hooks to the pppd startup and shutdown to keep track of the desired VPN state. These files should be owned by root, and have world read/execute permissions (
sudo chmod 755 /etc/ppp/ip-*).
#!/bin/sh echo true > /var/run/reconnect_vpn chmod 644 /var/run/reconnect_vpn
/etc/ppp/ip-down: for OS X 10.9.5 and below
#!/bin/sh tail /var/log/ppp.log | grep '\[DISCONNECT\]' if [ $? == 0 ] ; then echo false > /var/run/reconnect_vpn fi
/etc/ppp/ip-down: for OS X 10.10 and above
#!/bin/sh tail /var/log/ppp.log | grep '\[TERMINATE\]' if [ $? == 0 ] ; then echo false > /var/run/reconnect_vpn fi
Then by modifying the AppleScript above, I was able to check the '/var/run/reconnect_vpn' status variable to determine whether to bring the VPN back up:
on idle tell application "System Events" tell current location of network preferences set myConnection to the service "VPN" set startOnLogin to true local doReconnect try set doReconnect to (do shell script "cat /var/run/reconnect_vpn") on error errMsg set doReconnect to startOnLogin end try if myConnection is not null and doReconnect then if current configuration of myConnection is not connected then connect myConnection end if end if end tell return 120 end tell end idle
As before, change the line
set myConnection to the service "VPN" to whatever your VPN is called. Also, on startup the 'reconnect_vpn' file doesn't exist, so I added a boolean (startOnLogin) to serve as the default when the file could not be found. I like to start immediately, but if you don't then change it to false.
I have a feeling that if you're the kind of person is this particular about VPN behavior, then you're also the kind of person who likes fumbling around until you find a solution and therefore this answer has no audience. But just in case, here it is. Hope it helps somebody.