Start a Network Connection from Shell (OSX)

Solution 1:

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."

Solution 2:

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.