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

后端 未结 5 636
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 12:32

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 applicatio         


        
相关标签:
5条回答
  • 2021-02-04 13:03

    The Problem: The issue was I got this error.

    Can’t get «class svce» "MY VPN" of «class locc» of «class netp» of application "System Events".
    
    System Events got an error: Can’t get service "My VPN" of current location of network preferences. (-1728)
    

    The Reason: I am using IKEv2 services. It seems the Apple Script (which I found all over the internet) fails to work with this. You can find details in the link below.

    The solution: Timac wrote a script to solve this problem. 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

    You can also dig deeper into the problem if you are that sort of person on that link. It gives details.

    If you are simply looking for a solution to Auto Connect your VPN, the VPNstatus app worked for me.

    0 讨论(0)
  • 2021-02-04 13:04

    use shell script instead:

    scutil --nc start "$service"    #connect
    scutil --nc stop "$service"     #disconnect
    
    0 讨论(0)
  • 2021-02-04 13:05
    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
    
    0 讨论(0)
  • 2021-02-04 13:09

    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.

    0 讨论(0)
  • 2021-02-04 13:20

    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
    
    0 讨论(0)
提交回复
热议问题