Start a Network Connection from Shell (OSX)

3

How can I start a VPN connection (it's already all configured) from a shell on OSX? I have looked through the man entry on net but I don't think that's it. launchctl might be more promising...

Edit: I've made the title more general because I think the question would apply to any kind of network connection.

Dan Rosenstark

Posted 2011-04-02T20:32:36.630

Reputation: 5 718

Answers

2

See this thread. Basically you can Applescript it. Here's a full example:

#!/bin/sh 
osascript <<END 
tell application "System Events"
   tell current location of network preferences
       set VPNservice to service "Your VPN Name"
       if exists VPNservice then connect VPNservice
   end tell
end tell
END

You can even make another script replacing the word "connect" for "disconnect."

Dan Rosenstark

Posted 2011-04-02T20:32:36.630

Reputation: 5 718

1

You can toggle network connections' state by using networksetup -setnetworkserviceenabled NameOfService on/off, e.g.

networksetup -setnetworkserviceenabled "AirPort" off

to disable the AirPort connection.


Enabling VPN connections this way doesn't connect them (AFAICT) though.

Daniel Beck

Posted 2011-04-02T20:32:36.630

Reputation: 98 421

Yes, -setairportpower is more specific to AirPort, but it's just an example. -setnetworkserviceenabled is more general. Also, they perform different actions: A disabled AirPort network connection cannot be powered on. – Daniel Beck – 2011-04-02T23:35:41.503

Thanks @Daniel Beck, I put in an answer myself, after a bit of thinking horizontally about it ;) – Dan Rosenstark – 2011-04-03T06:41:28.753