Last active
January 24, 2022 08:56
-
-
Save andreassiegel/4792195cf1c9425ae09be6b3f23c908c to your computer and use it in GitHub Desktop.
Utility for VPN connections on Mac OS
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/bash | |
# | |
# Provides some basic utilities for VPN connections. | |
readonly cmd="${1}" | |
readonly vpn="${2}" | |
err() { | |
echo "$*" >&2 | |
} | |
validateCmd() { | |
if [[ -z "${cmd}" ]]; then | |
err "Command must be provided: [connect, disconnect, status, list]" | |
exit 1 | |
fi | |
} | |
validateVpnName() { | |
if [[ -z "${vpn}" ]]; then | |
err "VPN name must be provided as second argument" | |
exit 1 | |
fi | |
} | |
isConnected() { | |
networksetup -showpppoestatus "${vpn}" | grep -qv "^connected$" | |
} | |
enterPassword() { | |
sleep 1 | |
osascript -e "tell application \"System Events\" to keystroke \"${1}\"" | |
osascript -e "tell application \"System Events\" to keystroke return" | |
} | |
connect() { | |
if ! isConnected; then | |
err "Already connected to '${vpn}'" | |
exit 1 | |
fi | |
local readonly password="$(security find-generic-password -s "${vpn}" -w)" | |
if [[ -z "${password}" ]]; then | |
err "Unable to find VPN password in keychain" | |
exit 1 | |
fi | |
scutil --nc start "${vpn}" | |
enterPassword "${password}" | |
} | |
disconnect() { | |
scutil --nc stop "${vpn}" | |
} | |
status() { | |
scutil --nc status "${vpn}" | |
} | |
list() { | |
scutil --nc list | |
} | |
main() { | |
validateCmd | |
case "${cmd}" in | |
connect) | |
validateVpnName | |
connect "${vpn}" | |
;; | |
disconnect) | |
validateVpnName | |
disconnect "${vpn}" | |
;; | |
status) | |
validateVpnName | |
status "${vpn}" | |
;; | |
list) | |
list | |
;; | |
*) | |
err "Unexpected cmd '${cmd}', must be one of [connect, disconnect, status, list]" | |
exit 1 | |
;; | |
esac | |
} | |
set -e | |
main "${@}" | |
set +e |
Same issue as @GamerGun . I recommend the adjustment of the enterPassword() like this:
enterPassword() {
sleep 1
osascript -e "tell application \"System Events\" to keystroke tab"
osascript -e "tell application \"System Events\" to keystroke \"${1}\""
osascript -e "tell application \"System Events\" to keystroke return"
# optional. Only relevant if your VPN is displaying a welcome banner.
sleep 1
osascript -e "tell application \"System Events\" to keystroke return"
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since Big Sur, it inserts the password into the "Account Name" field (probably because the focus is on that field by default). Any idea how to resolve that?