Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a macbook pro with mavericks running. I am looking for a way to connect to a VPN network in terminal.

The reason why I want to do this is that I want to write a small program in Python which automatically detects the fastest VPN server among 30 servers. This is a self-motivated practicing project so I think I'll stick to Python language. So I break down the task and think the program may need to connect to one of the server first and after that, run a speed test.

So I'm now stuck in this first step because I realised establishing a VPN connection seems to be under system level because I can't find pre-written VPN module in python. So I guess it will be like I tell Python to tell system shell to connect to a VPN server.

As I poking around and I found a command by typing apropos vpn. It's called vpnagent. But man vpnagent doesn't provide useful information nor which vpnagent tells me the utility is not installed in my Mac. Another interesting thing I found was pppd but setting up the configuration file was very frustrating. I didn't manage to do that.

So is there a way of connecting to VPN using terminal? In addition, since I'm new to programming, any comment on my project is also welcome. Thank you in advance.

share|improve this question
up vote 4 down vote accepted

You can use these wonderful bash functions from @slhck at Super User:

To connect to different VPNs, have multiple VPNs in Network.prefpane.

function vpn-connect {
/usr/bin/env osascript <<-EOF
tell application "System Events"
        tell current location of network preferences
                set VPN to service "UniVPN" -- your VPN name here
                if exists VPN then connect VPN
                repeat while (current configuration of VPN is not connected)
                    delay 1
                end repeat
        end tell
end tell
function vpn-disconnect {
/usr/bin/env osascript <<-EOF
tell application "System Events"
        tell current location of network preferences
                set VPN to service "UniVPN" -- your VPN name here
                if exists VPN then disconnect VPN
        end tell
end tell

Don't forget to change the name of the VPN.

share|improve this answer
Thank you it works. But it becomes unhandy that I need to change the name of the VPN in ~/.bash-profile every time I need to conned to another vpn. Is there a way of adding argument into the function, so i can call like vpn-connect UniVPN? – Choushishi Apr 21 '14 at 19:14
@Choushishi You can just create multiple functions with different names and different VPNs. Duplicate the function and change the function name on the first line and the VPN name. – grgarside Apr 21 '14 at 19:17
Thanks this is a good solution. I think I will still try to find a way to add arguments to make it more elegant – Choushishi Apr 21 '14 at 19:32
@Choushishi you can make it take an argument just by replacing UniVPN by $1. (while keeping the double-quotes) – Timothée Boucher Oct 19 '14 at 23:10

scutil should be all you need.

scutil --nc start <service name>

So your Python script to connect to each in turn may include something like this:

import re
from subprocess import call, check_output

vpns_string = check_output(["scutil", "--nc", "list"]) # lists all VPN services

vpns = re.findall('"(.+)"', vpns_string) # service names are double-quoted

for vpn in vpns:
  call(["scutil", "--nc", "start", vpn]) stuff with your connection, test speed etc.
  call(["scutil", "--nc", "stop", vpn])

You can optionally specify username, password & secret to connect with - see scutil --nc help for usage.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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