Jump to content

El Capitan VPN Connect


Recommended Posts

I'm trying to get the VPN window to pop up with El Capitan.  This is the basic version of the code and it's just stopped working under 10.11.

set theService to "Secure VPN Connection"

tell application "System Events"
  tell network preferences
    connect service theService
  end tell
end tell

The weird thing is that if you change theService in that snippet to something that doesn't actually exist in your system then it will give you an error.  If you just run the code with a valid value for theService then it returns null and doesn't give any error nor does it trigger the window to pop up.

 

Any help would be appreciated.

Link to comment

I'm trying to get the VPN window to pop up with El Capitan.  This is the basic version of the code and it's just stopped working under 10.11.

set theService to "Secure VPN Connection"

tell application "System Events"
  tell network preferences
    connect service theService
  end tell
end tell

The weird thing is that if you change theService in that snippet to something that doesn't actually exist in your system then it will give you an error.  If you just run the code with a valid value for theService then it returns null and doesn't give any error nor does it trigger the window to pop up.

 

Any help would be appreciated.

 

Yes, El Capitan broken network service AppleScript.

Link to comment

Actually it looks like you can do it with a shell script.  Alfred passes in the first param from the command to the script which is then inputted into the VPN prompt window if you're using 2 factor auth or a password or something.  Here's the shell script from my workflow:

on alfred_script(q)
	set theConnection to "My VPN Connection" -- Change this to the name of your VPN connection
	set connectionWindowTitle to "VPN Connection"
	set theApp to "UserNotificationCenter"
	set thePassword to q
	open_vpn_connection(theConnection, thePassword, theApp, connectionWindowTitle)
end alfred_script

-- Find a window containing specific static text, in a given application
on find_window_by_static_text(appname, staticText)
	log "Searching " & appname & " for " & staticText
	tell application "System Events"
		set allApps to (get name of every application process) -- get all apps
		if allApps contains appname then -- find the app if it's running
			set allWin to (get every window of application process appname) -- get all the windows for our app
			set numWin to count allWin -- count the number of windows
			repeat with winNum from 1 to numWin
				set aWin to window winNum of application process appname
				set allText to (get value of every static text of aWin)
				if allText contains staticText then
					log "fwbst winnum: " & winNum
					return winNum
				end if
			end repeat
		end if
	end tell
	return null
end find_window_by_static_text

-- Open a given VPN connection and enter the password
on open_vpn_connection(theService, thePassword, theApp, connectionWindowTitle)
	set scutilCommand to "scutil --nc start '" & theService & "'"
	do shell script scutilCommand
	
	if (thePassword is null) or (thePassword = "") then
		-- No need to do anything else with the window here...
		return null
	end if
	
	delay 2
	repeat with x from 1 to 20
		log "vc loop " & x
		delay 0.1
		set winNum to find_window_by_static_text(theApp, connectionWindowTitle)
		log "winNum is: " & winNum
		if winNum is not null then
			exit repeat
		end if
	end repeat
	
	if winNum is null then
		display dialog "Could not find the VPN Connection window"
		return null
	end if
	
	tell application theApp to activate
	tell application "System Events"
		perform action "AXRaise" of item winNum of (get every window of application process theApp)
		keystroke thePassword
		key code 36
	end tell
end open_vpn_connection
Edited by _patrick
Link to comment
  • 3 weeks later...

Because I need to use a onetime token to connect to my VPN I need to enter the password manually and therefor I did a  versions of  the same script that will wait for the status of service to change like this:

set VPNserviceName to "My VPN Connection" -- Change this to your VPN connection name
set scConnectCommand to "scutil --nc start '" & VPNserviceName & "'"

if "Disconnected" is equal to getVPNServiceStatus(VPNserviceName) then
	log "Running command " & scConnectCommand
	do shell script scConnectCommand
	repeat until not "Connecting" is equal to getVPNServiceStatus(VPNserviceName)
		log "Waiting for service to be connected"
		delay 2
	end repeat
end if

on getVPNServiceStatus(VPNserviceName)
	return do shell script "scutil --nc status '" & VPNserviceName & "' | head -1"
end getVPNServiceStatus

Just though I would share. :-)

 

 

Link to comment
  • 2 weeks later...

I don't mean to be rude in asking for free labor, but is there any chance someone might package this updated version? Alternately, could you point me to a step-by-step process for workflow creation that would clarify what I need to do in terms of workflow/script creation to make this work? Thanks.  

Link to comment
  • 3 weeks later...

Hi

 

The upside shell script from Qvarnen works very well, it substitutes the not more functioning applescript with «tell application system events» for connecting VPN profiles in the built-in VPN Client..
UNFORTUNATELY, it works only with IPSec profiles. As soon as I configure a VPN profile with IKEv2 and use the concerning name in the Script, it does not work any more.

By connecting the IKEv2 profile manually, it works. But not with this Script.
Which could be the reason for this problem?

Thanks
knapzh

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...