I use the built-in VPN on Mac OS X 10.7.3. It's IPSec, FWIW. From time to time, it disconnects (probably an issue with my corporate server). Is there a way to get it to automatically reconnect? Sometimes I don't notice for a while, which is kinda annoying.
You could use the following AppleScript, save it as an application and set it to be a agent (no dock icon).
This script will setup a VPN connection when there is none. Therefore, it should also reconnect shortly after your connection drops. You can change the interval to check your VPN connection, it's 120 seconds in the script.
on idle tell application "System Events" tell current location of network preferences set myConnection to the service "VPN University" if myConnection is not null then if current configuration of myConnection is not connected then connect myConnection end if end if end tell return 120 end tell end idle
I've explained how to set this up in this answer.
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.
There's an app that does it called VPN Auto-Connect (Mac App Store link). It's $0.99.
Once started, it lives in your menu bar; when you use it to turn VPN "on", it will monitor a VPN connection profile you set up in OS X's Network preference pane and ensure you always remain connected to it. VPN Auto-Connect's menu-bar icon provides a list of all the VPN connections you've defined and lets you choose which one to always connect to.
We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.
A new option is VPN Monitor available on the AppStore. It allows for more customization, like auto-login, cycling through VPN connections, etc. At least OS X 10.9 Mavericks required.
Script auto-reconnect any dropped VPN service.
I had been using rjarvis2010's solution but I wasn't quite happy with it.
I have many different VPN services I connect to, so I wanted a script that would automatically reconnect any VPN I was connected to.
on idle tell application "System Events" tell location "Uni" of network preferences -- keep checking for VPN name until a VPN is connected set empty to true repeat until empty is false try -- set variable "myVPN" to the name of the service that is connected and is a VPN set myVPN to get name of first service whose (kind is greater than 11 and kind is less than 17) and connected of current configuration is true set empty to false on error set empty to true delay 15 end try end repeat -- doReconnect is a file that reads from the ppp.log and contains "true" by default, "false" if the vpn service was manually disconnected recently local doReconnect set doReconnect to (do shell script "cat /var/run/reconnect_vpn") repeat while doReconnect contains "true" set ConfProp to get current configuration of service myVPN if connected of ConfProp is false then delay 1 set doReconnect to (do shell script "cat /var/run/reconnect_vpn") if doReconnect contains "true" then try connect service myVPN on error errorMessage end try else exit repeat end if end if delay 5 end repeat end tell end tell return 1 end idle
To make it work for you, you need to replace
Uniwith whatever your location's name is
Once you're done, save it as an application and place it in your Login Items and you're good to go.
Also, and this is important, you need to set up the PPP hooks as described in rjarvis2010's solution
Also, I would recommend you don't try this connecting multiple VPNs simultaneously. To stop this script you might need to force quit it through the activity monitor (as the repeat loops don't allow it to receive external input at times).
I'm using different approach to keep my VPN connection alive.
This involves getting a very simple
launchd daemon running which allows maintaining VPN connection without any user logged in (this also avoid VPN disconnection popups when resuming from sleep mode).
launchd plist file from terminal :
sudo nano /Library/LaunchDaemons/my.vpn.connector.plist
and enter following content :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <dict> <key>NetworkState</key> <true/> </dict> <key>Label</key> <string>my.vpn.connector</string> <key>ProgramArguments</key> <array> <string>bash</string> <string>-c</string> <string>(test $(networksetup -showpppoestatus MyVPN) = 'disconnected' && ping -o my.vpn.server.url && networksetup -connectpppoeservice MyVPN) ; sleep 10</string> </array> </dict> </plist>
You can use following command to start your daemon and test:
launchctl load /Library/LaunchDaemons/my.vpn.connector.plist
This way, you have a daemon running for all users, connection attempted only when internet connection is available. Also, VPN is reconnected automatically when internet connection is back…
I find this method the most robust as it allows a vpn connect to be made automatically, before a user logs in (useful for servers).
Updates for Yosemite (OSX 10.10)
<key>KeepAlive</key> <dict> <key>NetworkState</key> <true/> </dict>
Is depreciated in Yosemite. You can use the following instead
networksetup -connectpppoeservice MyVPN
No longer works in Yosemite. You can use this instead
scutil --nc start MyVPN
I saw that there are a few answers on here that use login hooks, those are deprecated in newer versions of OS X in favor of Launch Agent and Launch Daemons.
I created a script and launch agent. 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).
The beauty of AppleScripts is that you can do almost anything with it and it's free, the downside is that they are usually not very responsive (polling at a fixed time interval) and lack features only native MAC OS X apps can have. A nice and new VPN auto reconnect app is "VPN Monitor" in the app store, it reconnects instantly the moment a VPN connection drops, is able to reconnect to a different VPN service if the preferred service is down, connects at start-up, keeps track of your downtime and uses a minimum of system resources to run in the background as a statusbar application. VPN Monitor