Skip to content

Instantly share code, notes, and snippets.

@GeriYatola
Last active August 29, 2015 14:23
Show Gist options
  • Save GeriYatola/f6ec9adf112bbb3dd8ca to your computer and use it in GitHub Desktop.
Save GeriYatola/f6ec9adf112bbb3dd8ca to your computer and use it in GitHub Desktop.
Bring OS X VPN connections up/down from bash
#! /bin/bash
# Bash functions for connecting/disconnecting to a VPN provider under OS X.
# Define additional routes at the end of vpn-connect if needed.
#
# Source:
# http://superuser.com/questions/358513/start-configured-vpn-from-command-line-osx
export VPN_NAME='My VPN' # Name of your VPN connection profile
export VPN_INTERFACE='ppp0' # VPN interface (fixed)
function vpn-connect {
/usr/bin/env osascript <<-EOF
tell application "System Events"
tell current location of network preferences
set VPN to service "${VPN_NAME}"
if exists VPN then connect VPN
repeat while (current configuration of VPN is not connected)
delay 1
end repeat
end tell
end tell
EOF
# Define additional routes for VPN_INTERFACE. Edit and uncomment if needed
# sudo route -n add 10.20.0.0/16 -interface ${VPN_INTERFACE}
}
function vpn-disconnect {
/usr/bin/env osascript <<-EOF
tell application "System Events"
tell current location of network preferences
set VPN to service "${VPN_NAME}"
if exists VPN then disconnect VPN
end tell
end tell
return
EOF
}
function vpn-toggle {
ifconfig ${VPN_INTERFACE} >/dev/null 2>&1 && vpn-disconnect || vpn-connect
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment