Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question

7 Answers 7

up vote 8 down vote accepted

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.

share|improve 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-*).

/etc/ppp/ip-up:

#!/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.

share|improve this answer

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.

share|improve this answer

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.

    
Welcome to Ask Different! Thanks for your answer, John! Can you add more information about the app you linked to? How does it answer the question? What specific features make it the app that the OP needs? –  daviesgeek Jul 18 '12 at 21:37

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).

create a 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' &amp;&amp;  ping -o my.vpn.server.url &amp;&amp; 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…

share|improve this answer

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.

share|improve this answer

Script to start auto-reconnect scripts when connected to individual services

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 made several copies of the VPN auto connection script each configured for the individual VPNs I'd want to connect to. The problem was that I didn't want each of those scripts to be running at the same time as that would cause my network to connect to several different VPNs at once. Neither did I want to have to start and quit each script manually. So I made a "VPN Script Activator" script to run alongside the original scripts. It's my first time dabbling with AppleScript, so it might be a bit clunky, but it gets the job done as far as I can tell.

on idle
    tell application "System Events"
        tell current location of network preferences
            set var to read POSIX file "/var/run/reconnect_vpn"

            #VPN 1
            if current configuration of the service "VPN 1 Name" is connected then
                tell application "System Events"
                    set ProcessList to name of every process
                    if "VPN 1 App Process Name" is not in ProcessList then
                        tell application "/path/to/your/vpn1/script.app" to launch
                    end if
                end tell
            end if
            if current configuration of the service "VPN 1 Name" is not connected then
                tell application "System Events"
                    set ProcessList to name of every process
                    if "VPN 1 App Process Name" is in ProcessList and var contains "false" then
                        tell application "/path/to/your/vpn1/script.app" to quit
                    end if
                end tell
            end if

            #VPN 2
            if current configuration of the service "VPN 2 Name" is connected then
                tell application "System Events"
                    set ProcessList to name of every process
                    if "VPN 2 App Process Name" is not in ProcessList then
                        tell application "/path/to/your/vpn2/script.app" to launch
                    end if
                end tell
            end if
            if current configuration of the service "VPN 2 Name" is not connected then
                tell application "System Events"
                    set ProcessList to name of every process
                    if "VPN 2 App Process Name" is in ProcessList and var contains "false" then
                        tell application "/path/to/your/vpn2/script.app" to quit
                    end if
                end tell
            end if

            #VPN 3
            #...et cetera...
            #make as many of these sections as you have scripts for VPNs.

        end tell
    end tell
end idle

What this does is start your script for each individual VPN Service when you connect to it, and quit that script whenever you disconnect from it (only if you actually disconnected from it yourself and it wasn't an error). It also checks whether the script is already running before trying to quit it and checks whether the script isn't running before trying to launch it.

To make it work for you, you need to replace

  • VPN 1 Name with whatever your service's name is
  • VPN 1 App Process Name with the script's process name (you can find what this is by running your script and checking with Activity Monitor, or you can check in the Bundle Contents sidebar in AppleScript Editor).
  • /path/to/your/vpn1/script.app (self-explanatory)

Once you're done, save it as an application and place it in your Login Items and you're good to go.

share|improve this answer

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

share|improve this answer
1  
Please have a look at the FAQ, especially the part about self-promotion –  patrix Aug 2 '14 at 7:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.