38

So far, I've been using the build-in VPN tool of Lion.

I would like to auto connect my VPN whenever I'm connected to the internet.

Is it possible to auto connect my VPN whenever I'm connected to the internet with the build in tool?

If auto connect is not possible with the build in tool, can you provide me with an alternative that is free?

4
  • Isn't there an option for this under "Advanced" in Network Settings? I don't have my Mac with me.
    – Matt
    Aug 8, 2012 at 15:25
  • Is there a way to do this with Viscocity, the Open VPN client?
    – user27065
    Aug 8, 2012 at 22:09
  • @christian Good question! I don't know. I've asked their support via Twitter and have not yet received an answer. But you can try to get a solution in their forum. So far, I've been using the amazing Shimo VPN client which supports OpenVPN and has this feature build in. However, this does not answer your question :).
    – gentmatt
    Sep 20, 2012 at 10:49
  • @christian The latest version of Viscosity has a setting for this. When you select a VPN profile and edit it, there is an option at the bottom to start this VPN service when Viscosity starts.
    – gentmatt
    Sep 20, 2012 at 12:55

2 Answers 2

44

Apple Script provides a good solution:

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

enter image description here enter image description here enter image description here enter image description here enter image description here

5
  • 2
    Great! Worked like a charm. I'm using Lion 10.7.2.
    – gentmatt
    Nov 28, 2011 at 14:26
  • 1
    still working perfectly on 10.12.3
    – Hofi
    Mar 10, 2017 at 12:02
  • Found via lifehacker: lifehacker.com/…
    – John Hunt
    Jan 29, 2019 at 16:15
  • 1
    This doesn't work for me on 10.14.6 ... I get an error message saying : "Can’t get «class svce» "ProtonVPN" of «class locc» of «class netp» of application "System Events". System Events got an error: Can’t get service "ProtonVPN" of current location of network preferences. (-1728) " ... ProtonVPN just happens to be the name that I assigned to my VPN connection in System Preferences. Does anyone know what might be wrong?
    – nckbrzg
    Feb 7, 2020 at 18:33
  • @nckbrzg check permission is allowed for your script app to access System Events.app in Prefs > Security > Privacy > Automation > your app > System Events.app checked May 11, 2022 at 14:33
8

Another way to go about this, is by creating a Configuration Profile (using Apple Configurator). Using this tool, recreate your VPN configuration and save the file. After you've created the file, open it up in a text editor and look for the following:

<key>VPNType</key>
<value>(...)</key>

Add the following below:

<key>OnDemandEnabled</key>
<integer>1</integer>
<key>OnDemandRules</key>
<array>
    <dict>
        <key>Action</key>
        <string>Connect</string>
        <key>InterfaceTypeMatch</key>
        <string>WiFi</string>
    </dict>
    <dict>
        <key>Action</key>
        <string>Connect</string>
        <key>InterfaceTypeMatch</key>
        <string>Cellular</string>
    </dict>
</array>

Now after you've installed this profile, a checkbox "Connect on demand" should be shown in the "Network" system preferences. Now macOS will keep your connection alive. Bonus feature: you can also add specific rules about when the VPN should connect or disconnect, depending on which Wifi network you're connected.

See Configuration Profile Reference on Apple's Developer Site for all on-demand rules. And see also the strongSwan wiki where some examples are shown.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .