Skip to content

Instantly share code, notes, and snippets.

@adgedenkers
Created October 11, 2012 18:16
Show Gist options
  • Select an option

  • Save adgedenkers/3874427 to your computer and use it in GitHub Desktop.

Select an option

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 "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
Copy link
Copy Markdown

cerniuk commented Oct 11, 2012

very nice! Thanks!

@stevesoto
Copy link
Copy Markdown

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
Copy link
Copy Markdown

drKreso commented May 25, 2014

Great!

@daich
Copy link
Copy Markdown

daich commented Jun 15, 2014

love it. Thanks a billion.

@antialias
Copy link
Copy Markdown

@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
Copy link
Copy Markdown

Excellent! Thanks!

@vladimirralev
Copy link
Copy Markdown

One more thanks!!

@harveyphan
Copy link
Copy Markdown

Thank you :)

@diethardsteiner
Copy link
Copy Markdown

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
Copy link
Copy Markdown

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
Copy link
Copy Markdown

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

@erikhansen
Copy link
Copy Markdown

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
Copy link
Copy Markdown

@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
Copy link
Copy Markdown

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
Copy link
Copy Markdown

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
Copy link
Copy Markdown

sweet! just what I was looking for ;)

@pujiaxun
Copy link
Copy Markdown

pujiaxun commented Aug 2, 2017

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

@sbine
Copy link
Copy Markdown

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
Copy link
Copy Markdown

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

@coneybeare
Copy link
Copy Markdown

Copy link
Copy Markdown

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
Copy link
Copy Markdown

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
Copy link
Copy Markdown

@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
Copy link
Copy Markdown

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
Copy link
Copy Markdown

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