In Mac OS X 10.11, Opening a VPN connection window with the command line gives me an error

我只是一个虾纸丫 提交于 2019-12-20 10:57:20

问题


On Mac OS X <= 10.10, I could run the following command to open a 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 would open the connection window (same as selecting the "LF VPN" network from the VPN dropdown). In El Capitan, however, 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 would one do the equivalent of this in El Capitan, and how can this be debugged?


回答1:


I'm using 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



回答2:


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



回答3:


use shell script instead:

scutil --nc start "$service"    #connect
scutil --nc stop "$service"     #disconnect



回答4:


Further to Oliver's answer, in macOS 10.12.6, the output of scutil --nc status has changed so that the match 'Connected' also matches for 'ConnectedCount'. Not sure in which version of macOS this changed.

I've made a slight change to the test to just look at the first line of output, which is really what 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 it helps others.



来源:https://stackoverflow.com/questions/32957121/in-mac-os-x-10-11-opening-a-vpn-connection-window-with-the-command-line-gives-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!