VPN via AppleScript works again in Mavericks as of last DP before GM #306

Open
CoMPaTech opened this Issue Oct 22, 2013 · 0 comments

Projects

None yet

1 participant

@CoMPaTech

As discussed on Twitter - VPN AppleScript which currently revolves around something like below. It connects to 'service' (e.g. Cisco VPNs) and might call in a secondary Viscosity (OpenVPN based) one.

Controlling VPN's broke both in ControlPlane and AppleScript somewhere in 10.8 and early 10.9 DP's but is now functional in AppleScript again.

Re-enabling OSX Native VPN support (Cisco/L2TP/PPTP) would be beneficial, adding Viscosity as an OpenVPN client would be 'nice'.

Way the script below works is it is fired (or killed) from ControlPlane and keeps checking for the connection to be up. I based it off different examples found while searching for a solution while CP itself was unable to control, which wasn't solvable that time :P

--
-- Applescript to start from 'controlplane' to determine location
-- and fire VPN and/or traffic deviation automatically
--
-- Currently will handle Cisco/IPsec native OSX VPNs and basic 
-- Viscosity OpenVPNs
--
on idle
    -- Get current location and set proper VPN connection name accordinly
    tell application "System Events"
        tell network preferences
            tell current location
                set activeLocation to the name
                if activeLocation = "Customer" then
                    set myConnection to service "VPN Work"
                else if activeLocation = "Work" then
                    set myConnection to service "VPN Work"
                else if activeLocation = "Home" then
                    -- If at home disconnect any VPNs
                    disconnect any
                    tell application "Viscosity" to disconnectall
                    -- Mind that controlplane will also kill the app
                    -- so the quit is 'just in case' :)
                    quit
                else
                    -- When unable to determine quit the script
                    quit
                end if
            end tell
        end tell
        -- Start connectivity to the VPN selected
        -- when applicable start the Viscosity link
        if not activeLocation = "Home" then
            tell network preferences
                set isConnected to connected of current configuration of myConnection
                if not isConnected then
                    connect myConnection
                end if
                if activeLocation = "Customer" then
                    if isConnected then
                        tell application "Viscosity" to connect "Secondary Tunnel"
                    end if
                end if

            end tell
        end if
        -- Sleep for 10 seconds then try again
        -- this ensures connections will stay more-or-less up
        -- (won't work for dongle/authenticated VPNs obviously) 
        return 10
    end tell
end idle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment