Skip to content

Instantly share code, notes, and snippets.

Created October 11, 2012 18:16
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save adgedenkers/3874427 to your computer and use it in GitHub Desktop.
Save adgedenkers/3874427 to your computer and use it in GitHub Desktop.
Toggle Connection to VPN on a Mac via AppleScript
tell application "System Events"
-- get current clipboard contents as a string
set CurrentClipboard to the clipboard as string
-- set the clipboad to your password
set the clipboard to "Y0urVPNPa$$w0rd"
-- start playing with the VPN
tell current location of network preferences
-- set the name of the VPN service from your Network Settings
set VPNService to service "VPN Connection 01"
-- determine current VPN connection status
set isConnected to connected of current configuration of VPNService
-- if connected, then disconnect
if isConnected then
disconnect VPNService
else -- otherwise, connect to the VPN
connect VPNService
-- wait 10 seconds before pasting in the password
delay 10
tell application id ""
-- paste clipboard contents into password box
keystroke "v" using {command down}
-- press "Enter"
keystroke (key code 36)
-- wait 10 seconds to connect
delay 10
-- determine current VPN connection status (after providing password)
set nowConnected to connected of current configuration of VPNService
-- if we're now connected ...
if nowConnected then
-- press "Enter" again to get rid of a dialog confirmation prompt, if one exists
keystroke (key code 36)
-- now, execute any other commands you want (ping a server to check its status, open mail, etc.)
end if
end tell
end if
end tell
-- now reset the clipboard to what it was before we started
set the clipboard to CurrentClipboard
end tell
Copy link

cerniuk commented Oct 11, 2012

very nice! Thanks!

Copy link

error "System Events got an error: Can’t get service "VPN Connection 01" of current location of network preferences." number -1728 from service "VPN Connection 01" of current location of network preferences

Running Mavericks

Copy link

drKreso commented May 25, 2014


Copy link

daich commented Jun 15, 2014

love it. Thanks a billion.

Copy link

@stevesoto: on line, 12, replace "VPN Connection 01" with the name of your VPN connection interface as it appears in the network preferences pane

Copy link

Excellent! Thanks!

Copy link

One more thanks!!

Copy link

Thank you :)

Copy link

I suggest adding the following (as I got some error messages from time to time with the above script):

Just before setting the current clipboard contents as as a string, clear the clipboard:

    set the clipboard to ""
on error err_message
    display dialog err_message
end try 

Copy link

lrettig commented Dec 23, 2015

This no longer works in 10.11, it produces the error, execution error: System Events got an error: Can’t get current configuration of service id "18E8C59B-C186-4669-9F8F-FA67D7AA6E53" of network preferences. (-1728). See for a workaround.

Copy link

scutil workaround works like a charm! Also for common Cisco IPsecs no Oauth is needed

Copy link

While this looks like a nifty script, I don't like the idea of storing the password in plain text. It would be great if someone could add the ability for the password to come from Keychain item. For now, I'm using this scutil solution:

Copy link

@antialias I'm seeing the same issue and I've substituted every field accordingly. I've also tried @erikhansen's scutil solution, but it always returns with No Service. This this an issue with OS X 10.11.4?

Copy link

Updated bash version:

IS_CONNECTED=$(test -z `scutil --nc status "$VPN" | grep Connected` && echo 0 || echo 1);

if [ $IS_CONNECTED = 1 ]; then
  scutil --nc stop "$VPN"
  scutil --nc start "$VPN"

Copy link

Oliver, that's neat, but 'if' can test the exit status of commands directly:


if scutil --nc status "$VPN" | grep -q Connected; then
  scutil --nc stop "$VPN"
  scutil --nc start "$VPN"

Copy link

sweet! just what I was looking for ;)

Copy link

pujiaxun commented Aug 2, 2017

Always No service.
Seems something wrong with IKEv2 on macOS 10.12
Anyone helps?

Copy link

sbine commented Aug 30, 2017

I ran into the "Can’t get current configuration" and "No service" errors after upgrading to Sierra. Duplicating the VPN configuration in Network Preferences (then using the new entry with scutil) fixed it for me.

Copy link

@sbine What do you mean by using new entry with scutil ? I'm stuck. :-/

Copy link

Copy link

ghost commented Mar 18, 2018

@spacebarrier I know it's been 6 months, but the terminal command
scutil --nc start "VPN_service_name"
will start the VPN service from a terminal command in High Sierra (obviously, replace VPN_service_name with the actual service name in the Network preference pane).

Replacing start with stop will, obviously, stop the VPN.

The terminal command can be run in AppleScript by using the syntax:
do shell script "scutil --nc start \"VPN_service_name\""
where the slashes are necessary to escape the quotes.

Copy link

tuxian0 commented Apr 5, 2018

Hello! Thank you for all these information. I've connected to VPN using Cisco AnyConnect. I tried with different possible names and server name as VPN, but I got "No service" error. Is there anyway we can list the available VPN connection names?

Copy link

@tuxian0 If you're using the Cisco AnyConnect client then none of this will work. You could try scripting the Cisco client directly, or see if your VPN configuration can be redone with the native macOS VPN client, configured in System Preferences -> Network.

Copy link

LwsBtlr commented Sep 6, 2020

Either this doesn’t work in 10.15 or it doesn’t work with the NordVPN IKE client. Iot doesn’t show up in the scutil —nc list command

Copy link

badesemowo commented Oct 6, 2020

@LwsBtlr , this is likely because you are using IKEv2 services. I had a similar issue trying to fix my VPN to auto reconnect. Below is the solution I eventually used. This may point you in the direction to solve your issue:

I simply downloaded the precompiled app here.

If you are interested in the source code that makes it work, you can find it on GitHub here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment