azun
azun

Reputation: 431

How to get IKEv2 VPN connection by AppleScript?

Does anyone experience AppleScript in working with IKEv2 network service?

In El Capitan, I can create an IKEv2 VPN connection and connect correctly. However AppleScript doesn't work with that kind of connection/service, it cannot get the service with name, it cannot list the connection from the service.

tell application "System Events"
   tell current location of network preferences
      set service_name to "IKEv2_connection_name"
      do shell script (do shell script "scutil --nc start \"" & service_name & "\"")
   end tell
end tell

And here is the error:

error "System Events got an error: No service" number 1

It appears that AppleScript cannot recognize the IKEv2 VPN connection. So I tried to run another script which to print out all the current internet connections in the system:

tell application "System Events"
   tell current location of network preferences
      set names to get name of every service
   end tell
end tell

The result shows all the network connections (including "Wi-Fi", "USB Ethernet", "Bluetooth PAN", "Thunderbolt Bridge", all VPN connections of type L2TP, PTPP, IPSec) but it doesn't list any IKEv2 connections although I have set a few of them and they're all working.

Upvotes: 2

Views: 3791

Answers (3)

Motsel
Motsel

Reputation: 548

This is not a direct answer to your question, but another way to accomplish the same;

This guy created an app (source available also) to do what scutil and AppleScript cannot : https://blog.timac.org/2018/0719-vpnstatus/

Upvotes: 2

OneWholeBurrito
OneWholeBurrito

Reputation: 492

I was able to get this to work using UI scripting. So far this script seems to work well, and I think I've made it about as user friendly as possible. It requires System Preferences to be opened at all times, but the window can be hidden. If System Preferences is closed when the script starts, the script will launch and then auto-hide System Preferences. Major inconvenience is that System Preferences is essentially locked to the Network pane while the script is running. Feel free to give it a try, let me know if you have any problems or suggestions! You'll need to replace "MY VPN SERVICE NAME" with the name of the VPN service you want to keep connected. You may also want to modify the delay at the bottom.

repeat while true
tell application "System Events"
    tell application process "System Preferences"
        if not (window 1 exists) then
            tell application "System Preferences"
                activate
            end tell
            repeat while not (menu bar 1 exists)
            end repeat
            repeat while not (menu "System Preferences" of menu bar 1 exists)
            end repeat
            repeat while not (menu item "Hide System Preferences" of menu "System Preferences" of menu bar 1 exists)
            end repeat
            delay 3
            click menu item "Hide System Preferences" of menu "System Preferences" of menu bar 1
        end if

        click menu item "Network" of menu "View" of menu bar 1

        tell window 1
            repeat while not (rows of table 1 of scroll area 1 exists)
            end repeat
            repeat with current_row in (rows of table 1 of scroll area 1)
                if value of static text 1 of current_row contains "MY VPN SERVICE NAME" then
                    select current_row
                    exit repeat
                end if
            end repeat

            repeat with current_button in (buttons in group 1)
                if name of current_button is equal to "Connect" then
                    click current_button
                    exit repeat
                end if
            end repeat
        end tell
    end tell
end tell
delay 60
end repeat

Upvotes: 2

Nicolai Kant
Nicolai Kant

Reputation: 1391

There are a few reports of this and it seems to be some changes in AppleScript made in OSX 10.10 which stopped services in connection object to list IKEv2 VPNs.

You are on the right path though, just don't use location:

tell application "System Events"
      set service_name to "IKEv2_connection_name"
      do shell script "scutil --nc start \"" & service_name & "\""
end tell

Based on this answer, where you can see more options for scutil as well:

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

Upvotes: 0

Related Questions