Last active
December 11, 2020 05:55
-
-
Save jakob-stoeck/fa4fd4d41f93a97afee662acedaceb3e to your computer and use it in GitHub Desktop.
Autofill Cisco VPN password saved in macOS keychain
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 | |
if [[ $# < 2 ]]; then | |
echo "Use password saved in macOS keychain for Cisco VPNs" | |
echo "$0 usage: myscript vpnname keychainitem [close_second_window]" | |
exit 1 | |
fi | |
VPNName=$1 # match the name of the VPN service to run | |
keychainItem=$2 # this name has to match "Account" for the entry you make in keychain | |
password=$(security find-generic-password -wl "$keychainItem") | |
# exit if keychain item is not accessible, the keychain will write an error message already | |
if [ $? != 0 ]; then exit 1; fi | |
# echo "Fetching VPN credentials from keychain item \"$keychainItem\"" | |
# echo "Using VPN service: \"$VPNName\"" | |
scutil --nc status "$VPNName" | grep -q "Disconnected" | |
if [ $? != 0 ]; then echo "Could not connect. Please check that the VPN name is written correctly and you are not already connected." 1>&2; exit 1; fi | |
scutil --nc start "$VPNName" | |
printf "Connecting ... " | |
trap "echo \"aborting\"; scutil --nc stop \"$VPNName\"; exit" SIGINT SIGQUIT | |
osascript <<EOF | |
tell application "System Events" to tell process "UserNotificationCenter" | |
repeat until window 1 exists | |
delay 0.05 | |
end repeat | |
tell application "System Events" | |
keystroke "$password" | |
keystroke return | |
end tell | |
keystroke return | |
end tell | |
EOF | |
# optional second information window | |
if [[ $3 ]]; then | |
osascript <<EOF | |
tell application "System Events" to tell process "UserNotificationCenter" | |
repeat until window 1 exists | |
delay 0.05 | |
end repeat | |
keystroke return | |
end tell | |
EOF | |
fi | |
printf "success\n" | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For macOS Big Sur it's needed an extra tab before entering password.