Last Updated: April 12, 2018
·
16.09K
· ernestom

Automatically connect to a VPN on startup in OSX

  • Create a file called connectvpn.applescript using AppleScript Editor with the following:

tell application "System Events"
    tell current location of network preferences
        set VPNService to service "MYVPNNAME"
        if exists VPNService then
            connect VPNService
        end if
    end tell
end tell
</code></pre>


Substitute "MYVPNNAME" with the name of your VPN service as it is shown in the settings app.
Save as application.
Add the saved .app to the boot items in the startup items in the users and group panel in the settings app. Don't forget to check the hide checkbox.

4 Responses
Add your response

Works well. To save as application, you need the "Export" menu option

over 1 year ago ·

Drawback is if the Applescript is unable to connect as the VPN service is down or your internet connection is not ready yet. The alternative is to use an App which will also help you to keep the connection up and reconnect in case the VPN service drops the connection. 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

over 1 year ago ·

That won't work with IKEv2 VPNs. Here's how you can do that using a different AppleScript

over 1 year ago ·

Im using Sierra so this works for me but i'd like to specify to only reconnect when the particular network is active. The following isn't quite functioning. Any idea's whats wrong?

on idle
set mySSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'"
if ((mySSID as string) is equal to "SSID NAME”) then
tell application "System Events"
tell current location of network preferences
set myConnection to the service “MYVPNNAME”
if myConnection is not null then
if current configuration of myConnection is not connected then
connect myConnection
end if
end if
end tell
return 4
end tell
end if
end idle

over 1 year ago ·