4

In my university, the only way to access Internet is to connect to a VPN server. I've configured the VPN on my Mac and it's working fine, but I need to manually connect to the VPN every time I connect to the University network. How can I configure my Mac so it automatically uses the VPN whenever I'm working inside the University network?

Edit:
Can I use AppleScript to achieve this? Theoretically, it should be fairly simple, something like this:

if NetworkAPI.connectedNetwork.SSID == "Specific SSID":
    NetworkAPI.getVPNConnection("VPN Name").connect()

(I added AppleScript tag to my question to attract people with expertise in AppleScript, but any other method is acceptable.)

6
  • What VPN Software are you using? Oct 13, 2015 at 7:12
  • @Garex The one built-in in OS X.
    – Behdad
    Oct 13, 2015 at 7:18
  • 1
    Ok, I found an app that reconnects you when the VPN connection dropped. It is called "VPN AutoConnect" for $1 in the App Store. I could not find any Opensource software for that. What I didn't try is to add a system route for a specific network over the VPN tunnel interface you defined in Network preferences. But for that you would need additional settings you cannot configure in Mac OS X Network preferences. I am used for VPN server connections when you try to connect to a specific IP range, the VPN tunnel is automatically built up. But this seems not to be possible with Mac OS X. Oct 13, 2015 at 13:04
  • @Garex Although VPN AutoConnect sound like an interesting app, but it doesn't do what I'm asking for. I was thinking something simple, like using AppleScript would do it, I don't know if it's capable of doing such thing.
    – Behdad
    Oct 13, 2015 at 13:09
  • 1
    I have the same problem and decided to finally solve it. Created a simple App that watches Wifi changes that I launch when I log in, it then executes Bash scripts that I can easily modify – app and example scripts on Github for those interested: github.com/p2/WifiWatch
    – Pascal
    Nov 25, 2016 at 14:47

1 Answer 1

5
+50

LifeHacker has answered your prayers (with Apple Script). You will obviously need to replace VPN University with the name of the service pertinent to you, and SSID University with the name of your university's SSID.

on idle
    set mySSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'"
    if ((mySSID as string) is equal to "SSID University") then
    tell application "System Events"
            tell current location of network preferences
            set myConnection to the service "VPN University"
            if myConnection is not null then
                if current configuration of myConnection is not connected then
                    connect myConnection
                end if
            end if
        end tell
        return 120
    end tell
    end if
end idle

You can save this as an application any way you want, I prefer to go through Automator, though Script Editor, AppleScript, or XCode are also capable of compiling AppleScript, save it as an application, mark it to start on log in, then if you want to be fancy, you can either tell XCode not to have its icon on the dock, or edit the .plst file in the application bundle as such:

<key>LSUIElement</key>
<string>1</string>

This little hack per user ianneub.

Note I have tested the applescript piece only on my early '11 15" MBP OS X 10.10. Your mileage may vary if you're running El Capitan (I bet it doesn't though). This post comes with no warranties of any kind, and may give you gonorrhea.

EDIT: Note I updated the original script to check for a specific SSID, then perform the actions. I'm on my WinXP machine right now, so this is an untested, but relatively safe change. I found the info on getting the current SSID here and info on string comparison here

4
  • Thanks, but I don't want my VPN to "reconnect" every time it's disconnected. I want it to connect whenever I'm connected to a certain network. I don't think your answer applies here. Also, I'm a little worried about gonorrhea. (By the way, what is MPB? 'MacPook, Bro'?)
    – Behdad
    Oct 15, 2015 at 7:33
  • Ah. I see. I misread your question. Adapting the script above should be simple enough, I'll take a look later. Oct 15, 2015 at 15:05
  • I won't visit my university in a few days (and there isn't any VPN servers I can connect to in my home network), so I can't test your solution, but I set the SSID to my home network and it tried to connect the VPN. I guess this is the correct solution. Thank you so much. (Also, there was a type and I fixed it.)
    – Behdad
    Oct 18, 2015 at 21:10
  • Thanks for the correction, I'll test at my university tomorrow to verify. Oct 18, 2015 at 21:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .