├── README.md ├── bash_completion.d └── osxvpncli └── osxvpncli /README.md: -------------------------------------------------------------------------------- 1 | # osxvpncli 2 | 3 | This is a wrapper to help you start and stop your macOS VPN connections from the command line. 4 | 5 | ## Installation 6 | 7 | The easiest method is via `homebrew`: 8 | 9 | ```ShellSession 10 | $ brew tap jonhiggs/homebrew-osxvpncli 11 | $ brew install osxvpncli 12 | $ osxvpncli --help 13 | ``` 14 | 15 | Alternatively you can just clone the git repository to your local system: 16 | 17 | ```ShellSession 18 | $ git clone https://github.com/jonhiggs/homebrew-osxvpncli.git 19 | $ /osxvpncli --help 20 | ``` 21 | 22 | ## Setup 23 | 24 | Before you can do anything, you must first create the VPN connections that you wish to manipulate though `System Preferences -> Network`. 25 | 26 | If you would like working tab-completion, you can use the completion script in `bash_completion.d/osxvpncli`. If you're not sure how to use bash_completion scripts, take a look at [this blog post](http://blog.jeffterrace.com/2012/09/bash-completion-for-mac-os-x.html). 27 | 28 | ## Usage 29 | 30 | Usage: 31 | osxvpncli [OPTION] [vpn_name] 32 | 33 | Options: 34 | connect Establish a new connection 35 | disconnect [vpn_name] Terminate established connections 36 | status [vpn_name] Show connection status 37 | 38 | ## Change Log 39 | 40 | ## Version 0.2.2 41 | 42 | - Renamed `vpn` to `osxvpncli` to avoid being overly rude with namespace. 43 | - Document installation with `homebrew`. 44 | 45 | ## Version 0.2.1 46 | 47 | - Disconnect all if connection isn't supplied. [#issue 5](https://github.com/jonhiggs/osxvpncli/issues/5) 48 | 49 | ## Version 0.2 50 | 51 | - Remove support for OS X Yosemite. 52 | - Refactor for simpler code 53 | -------------------------------------------------------------------------------- /bash_completion.d/osxvpncli: -------------------------------------------------------------------------------- 1 | __osxvpncli_connected() { 2 | osxvpncli status | awk ' 3 | BEGIN { FS="\t" } 4 | /Connected$/ { 5 | gsub(/\ +Connected/, "") 6 | gsub(/\ /, "\\ "); 7 | print $1 8 | } 9 | ' 10 | } 11 | 12 | __osxvpncli_disconnected() { 13 | osxvpncli status | awk ' 14 | BEGIN { FS="\t" } 15 | /Disconnected$/ { 16 | gsub(/\ +Disconnected/, "") 17 | print $1 18 | } 19 | ' 20 | } 21 | 22 | __osvpncli() { 23 | local command="$1" 24 | local word="$2" 25 | local context="$3" 26 | 27 | contexts=$(echo -e "status\nconnect\ndisconnect") 28 | 29 | case ${context} in 30 | "osxvpncli") options=$contexts ;; 31 | "connect") options=$(__osxvpncli_disconnected) ;; 32 | "disconnect") options=$(__osxvpncli_connected) ;; 33 | "status") options="" ;; 34 | *) options="" 35 | esac 36 | 37 | local IFS=$'\n' 38 | COMPREPLY=($(compgen -W "${options}" -- ${word})) 39 | if [ -n "$COMPREPLY" ]; then 40 | COMPREPLY=($(printf "%q\n" "${COMPREPLY[@]}")) 41 | fi 42 | return 0 43 | } 44 | 45 | complete -F __osvpncli osxvpncli 46 | 47 | # vim: ft=sh 48 | -------------------------------------------------------------------------------- /osxvpncli: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | function usage() { 3 | cat < Establish a new connection 15 | disconnect [vpn_name] Terminate established connections 16 | status [vpn_name] Show connection status 17 | EOF 18 | exit 1 19 | } 20 | 21 | function connections() { 22 | scutil --nc list | awk ' 23 | BEGIN { FS="\"" } 24 | /^\*/ { 25 | if ( /PPP:L2TP.$/ ) { print $2 } 26 | if ( /IPSec.$/ ) { print $2 } 27 | if ( /PPP:PPTP.$/ ) { print $2 } 28 | } 29 | ' | sort 30 | } 31 | 32 | function connected() { 33 | connection_status \ 34 | | awk -F$'\t' '/Connected$/ { print $1 }' 35 | } 36 | 37 | function connection_status() { 38 | IFS=$'\n' 39 | for c in ${1:-$(connections)}; do 40 | echo -e "$c\t$(scutil --nc status "$c" | head -n1)" 41 | done 42 | } 43 | 44 | f=$1 45 | shift 46 | 47 | case $f in 48 | "connect") 49 | [[ $# -eq 0 ]] && echo "VPN not provided" 1>&2 && exit 1 50 | scutil --nc start "$@" 51 | ;; 52 | 53 | "disconnect") 54 | if [[ $# -eq 0 ]]; then 55 | IFS=$'\n' 56 | for c in $(connected); do scutil --nc stop "${c}"; done 57 | else 58 | scutil --nc stop "$@" || exit 1 59 | fi 60 | ;; 61 | 62 | "status") connection_status "$@" | column -ts $'\t' ;; 63 | *) usage ;; 64 | esac 65 | --------------------------------------------------------------------------------