Skip to content

Instantly share code, notes, and snippets.

@patricksimpson
Created April 25, 2017 14:02
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 patricksimpson/a5fc6dafe292a310808525b95324978a to your computer and use it in GitHub Desktop.
Save patricksimpson/a5fc6dafe292a310808525b95324978a to your computer and use it in GitHub Desktop.
keychainItem=vpn # this name has to match "Account" for the entry you make in keychain
VPNName="" # match the name of the VPN service to run
function isnt_connected () {
scutil --nc status "$VPNName" | sed -n 1p | grep -qv Connected
}
get_pw () {
security 2>&1 >/dev/null find-generic-password -ga $keychainItem \
|ruby -e 'print $1 if STDIN.gets =~ /^password: "(.*)"$/'
}
echo "fetching VPN credentials from keychain account \"$keychainItem\""
echo "Using VPN service: $VPNName"
if isnt_connected $VPNName; then
echo "connecting to VPN"
scutil --nc start "$VPNName"
sleep 2.7
osascript -e "tell application \"System Events\" to keystroke \"$(get_pw)\""
osascript -e "tell application \"System Events\" to keystroke return"
sleep 2
else
echo "Already Connected to VPN..."
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment