Last active
July 23, 2017 20:48
-
-
Save patrickyoung/2ebe92c0dc5cb4ca0c47a419144813df to your computer and use it in GitHub Desktop.
This script provides a simple API to control VPN connections in macOS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This script provides a simple API to control VPN connections in macOS. | |
DEFAULT_VPN="$(scutil --nc list | awk '/IPSec/ {print $5}' | sed 's/\"//g')" | |
COMMAND=$1 | |
VPN_NAME=${2:-${DEFAULT_VPN}} | |
function turn_on_vpn() { | |
echo $(scutil --nc start ${VPN_NAME}) | |
vpn_status='' | |
get_vpn_status vpn_status | |
echo "VPN Status: ${vpn_status}" | |
if [ "${vpn_status}" == "Connected" ] | |
then | |
exit 0; | |
else | |
exit 1; | |
fi | |
} | |
function turn_off_vpn() { | |
echo $(scutil --nc stop "${VPN_NAME}") | |
vpn_status='' | |
get_vpn_status vpn_status | |
echo "VPN Status: ${vpn_status}" | |
if [ "${vpn_status}" == "Disconnected" ] | |
then | |
exit 0; | |
else | |
exit 1; | |
fi | |
} | |
function get_vpn_status() { | |
vpn_status=$(scutil --nc status "${VPN_NAME}" | sed -n 1p) | |
eval "$1=${vpn_status}" | |
} | |
function display_vpn_status() { | |
vpn_status='' | |
get_vpn_status vpn_status | |
echo "${vpn_status}" | |
} | |
function list_vpn_services() { | |
echo "$(scutil --nc list | awk '/IPSec/ {print $5}' | sed 's/\"//g')" | |
} | |
case ${COMMAND} in | |
up) | |
turn_on_vpn | |
;; | |
down) | |
turn_off_vpn | |
;; | |
status) | |
display_vpn_status | |
;; | |
list) | |
list_vpn_services | |
;; | |
*) | |
echo "usage: vpn <command> [VPN Name]" | |
echo "" | |
echo "These are the VPN commands available:" | |
echo "" | |
echo " up Bring up the VPN connection" | |
echo " down Stop the VPN connection" | |
echo " status Get the VPN connection status" | |
echo " list List VPN services configured" | |
echo "" | |
echo "Available VPNs:" | |
echo "" | |
echo "$(list_vpn_services)" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment