Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 "com.apple.systemevents"
-- 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
@cerniuk

This comment has been minimized.

Copy link

@cerniuk cerniuk commented Oct 11, 2012

very nice! Thanks!

@stevesoto

This comment has been minimized.

Copy link

@stevesoto stevesoto commented Nov 11, 2013

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

@drKreso

This comment has been minimized.

Copy link

@drKreso drKreso commented May 25, 2014

Great!

@daich

This comment has been minimized.

Copy link

@daich daich commented Jun 15, 2014

love it. Thanks a billion.

@antialias

This comment has been minimized.

Copy link

@antialias antialias commented Jun 23, 2014

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

@diethardsteiner

This comment has been minimized.

Copy link

@diethardsteiner diethardsteiner commented Jun 30, 2014

Excellent! Thanks!

@vladimirralev

This comment has been minimized.

Copy link

@vladimirralev vladimirralev commented Aug 18, 2014

One more thanks!!

@harveyphan

This comment has been minimized.

Copy link

@harveyphan harveyphan commented Aug 27, 2014

Thank you :)

@diethardsteiner

This comment has been minimized.

Copy link

@diethardsteiner diethardsteiner commented Oct 17, 2014

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:

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

This comment has been minimized.

Copy link

@lrettig 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 http://stackoverflow.com/questions/32957121/in-mac-os-x-10-11-opening-a-vpn-connection-window-with-the-command-line-gives-m for a workaround.

@TaiSHiNet

This comment has been minimized.

Copy link

@TaiSHiNet TaiSHiNet commented Feb 5, 2016

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

@erikhansen

This comment has been minimized.

Copy link

@erikhansen erikhansen commented Mar 1, 2016

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: http://superuser.com/a/934577

@JamshedVesuna

This comment has been minimized.

Copy link

@JamshedVesuna JamshedVesuna commented May 7, 2016

@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?

@OliverJAsh

This comment has been minimized.

Copy link

@OliverJAsh OliverJAsh commented Jul 1, 2016

Updated bash version:

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

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

This comment has been minimized.

Copy link

@bazzargh bazzargh commented Jan 12, 2017

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

VPN="YOUR_VPN_NAME"

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

This comment has been minimized.

Copy link

@ryanemmm ryanemmm commented Apr 29, 2017

sweet! just what I was looking for ;)

@pujiaxun

This comment has been minimized.

Copy link

@pujiaxun pujiaxun commented Aug 2, 2017

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

@sbine

This comment has been minimized.

Copy link

@sbine 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.

@spacebarrier

This comment has been minimized.

Copy link

@spacebarrier spacebarrier commented Sep 19, 2017

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

@coneybeare

This comment has been minimized.

Copy link

@coneybeare coneybeare commented Sep 30, 2017

@ghost

This comment has been minimized.

Copy link

@ghost 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.

@tuxian0

This comment has been minimized.

Copy link

@tuxian0 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?

@NReilingh

This comment has been minimized.

Copy link

@NReilingh NReilingh commented Oct 27, 2018

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

@LwsBtlr

This comment has been minimized.

Copy link

@LwsBtlr 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

@badesemowo

This comment has been minimized.

Copy link

@badesemowo 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.
https://blog.timac.org/2018/0719-vpnstatus/

If you are interested in the source code that makes it work, you can find it on GitHub here: https://github.com/Timac/VPNStatus

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