I need to deploy a couple of remote computers and I need them to always connect to the VPN whenever there is internet connectivity. Is this possible? If so, how?

link|improve this question

1  
I have a script that notifies me about network configuration changes, and another script that connects to a specific VPN. I might be able to combine those. I'm at work right now, maybe in 6 hours or so. – Daniel Beck Jan 11 at 14:48
that would be glorious! – Chase Florell Jan 11 at 14:53
Didn't manage to do it today, I'll try tomorrow. – Daniel Beck Jan 11 at 22:24
feedback

1 Answer

up vote 2 down vote accepted

First, you need to set up locationchanger, a script that is launched whenever (possibly) the network settings have changed.

I've been using a heavily modified version (no location detection, just show a Growl notification whenever anything is changed, i.e. in the section "do some stuff here that needs to happen after every network change") for over a year, and it's quite reliable. Sometimes it detects two changes in quick succession, but that's it. Since your needs are more along the defaults, it will probably work even better for you.


Open AppleScript Editor, and paste the following code:

tell application "System Events"
    tell current location of network preferences
        if exists service "Displayed Name" then
            set VPNservice to service "Displayed Name"
            if connected of first configuration of VPNservice then
                disconnect VPNservice
            else
                connect VPNservice
            end if
        else
            display alert "Could not find VPN connection"
        end if
    end tell
end tell

Replace Displayed Name by the name of your VPN connection in System Preferences » Network.

Save as application, e.g. /Applications/Utilities/Toggle VPN.app

Then, edit the locationchanger script and add

open "/Applications/Utilities/Toggle VPN.app"

If you don't want an icon to bounce in the Dock whenever you change something, you san save the AppleScript as script instead, and execute it from locationchanger like this:

osascript "/path/to/Toggle VPN.scpt"

This solution will require some experimentation on your part. The reason is obvious: Connecting or disconnecting a VPN is a change in the network configuration. So given what I wrote here, it's possible that you connect to a network, the script connects to the VPN, the VPN configuration change triggers the script again, and it disconnects.

Of course, you can simply remove the line that says disconnect VPNservice and try to. It really depends on the exact behavior you want. But these are the building blocks.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.