On Mac OS X 10.11, opening a VPN connection window with a command line gives me an error

On Mac OS X <= 10.10, I can run the following command to open the VPN connection window:

function go-vpn { /usr/bin/env osascript <<-EOF tell application "System Events" tell current location of network preferences set VPN to service "LF VPN" if exists VPN then connect VPN repeat while (current configuration of VPN is not connected) delay 1 end repeat end tell end tell EOF } 

This will open a connection window (the same as selecting the "LF VPN" network from the VPN drop-down list). However, in El Capitan, I get the following 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) 

How can the equivalent of this be done in El Capitan and how can it be debugged?

Annotated Screenshot

+7
applescript osx-elcapitan macos
source share
4 answers

I use scutil instead and it works flawlessly on OS X 10.11

 set vpn_name to "'VPN Connection Name'" set user_name to "my_user_name" set otp_token to "XYZXYZABCABC" tell application "System Events" set rc to do shell script "scutil --nc status " & vpn_name if rc starts with "Connected" then do shell script "scutil --nc stop " & vpn_name else set PWScript to "security find-generic-password -D \"802.1X Password\" -w -a " & user_name set passwd to do shell script PWScript -- installed through "brew install oath-toolkit" set OTPScript to "/usr/local/bin/oathtool --totp --base32 " & otp_token set otp to do shell script OTPScript do shell script "scutil --nc start " & vpn_name & " --user " & user_name delay 2 keystroke passwd keystroke otp keystroke return end if end tell 
+14
source share
 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 
+6
source share

use shell script instead:

 scutil --nc start "$service" #connect scutil --nc stop "$service" #disconnect 
+3
source share

In response to Oliver's answer on macOS 10.12.6, the output of scutil --nc status has changed, so that the match “Connected” also matches “ConnectedCount”. Not sure which version of macOS this has changed.

I modified the test a bit to just look at the first line of output, which really needs to be checked.

 VPN="YOUR_VPN_NAME" IS_CONNECTED=$(test -z `scutil --nc status "$VPN" | head -n 1 | grep Connected` && echo 0 || echo 1); if [ $IS_CONNECTED = 1 ]; then scutil --nc stop "$VPN" else scutil --nc start "$VPN" fi 

This works for me on macOs 10.12.6. Hope this helps others.

0
source share

All Articles