Skip to content

Instantly share code, notes, and snippets.

@jakob-stoeck
Last active December 11, 2020 05:55
Show Gist options
  • Save jakob-stoeck/fa4fd4d41f93a97afee662acedaceb3e to your computer and use it in GitHub Desktop.
Save jakob-stoeck/fa4fd4d41f93a97afee662acedaceb3e to your computer and use it in GitHub Desktop.
Autofill Cisco VPN password saved in macOS keychain
#!/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
@kikeenrique
Copy link

For macOS Big Sur it's needed an extra tab before entering password.

    tell application "System Events"
        keystroke (ASCII character 9)
        keystroke "$password"
        keystroke return
    end tell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment