Skip to content

Instantly share code, notes, and snippets.

@djui
Created October 18, 2014 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djui/aed8c739782644b62494 to your computer and use it in GitHub Desktop.
Save djui/aed8c739782644b62494 to your computer and use it in GitHub Desktop.
Takes a password, generates the TOTP token and starts the OSX VPN
#!/bin/sh
## Dependencies: $ brew install oath-toolkit
function connect {
service="$1"
password='123456'
secret=$(cat $HOME/.google_authenticator)
totp=$(oathtool --base32 --totp $secret)
## Attempt 1
# scutil --nc start "$service" --user "$account" --password "$password" --secret "$secret"
## Attempt 2
/usr/bin/env osascript <<EOF
tell application "System Events"
set CurrentClipboard to the clipboard as string
set the clipboard to "$password$totp"
tell current location of network preferences
set VPN to service "$service"
if exists VPN then connect VPN
delay 5
tell application id "com.apple.systemevents"
keystroke "v" using {command down}
delay 1
keystroke (key code 36)
end tell
repeat while (current configuration of VPN is not connected)
delay 1
end repeat
end tell
end tell
EOF
## Attemp 3
# pbcopy <<< $password$totp
#networksetup -connectpppoeservice "$service"
}
function disconnect {
service="$1"
## Attempt 1
# scutil --nc stop "$service"
## Attempt 2
/usr/bin/env osascript <<EOF
tell application "System Events"
tell current location of network preferences
set VPN to service "$service"
if exists VPN then disconnect VPN
end tell
end tell
EOF
## Attempt 3
networksetup -disconnectpppoeservice "$service"
}
name="my VPN"
action=connect
if [ $1 ] ; then
case $1 in
"-c"|"--connect" |"connect" ) action=connect ; shift ;;
"-d"|"--disconnect"|"disconnect") action=disconnect ; shift ;;
"-n"|"--name" |"name" ) name="$2" ; shift ; shift ;;
*) echo "Usage: vpn [--connect|--disconnect|--name NAME]"
esac
fi
$action "$name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment