Skip to content

Instantly share code, notes, and snippets.

@kikeenrique
Forked from jakob-stoeck/vpn.sh
Last active December 10, 2020 09:51
Show Gist options
  • Save kikeenrique/c1f874e761c672918addab1096ec9144 to your computer and use it in GitHub Desktop.
Save kikeenrique/c1f874e761c672918addab1096ec9144 to your computer and use it in GitHub Desktop.
Autofill Cisco VPN password saved in macOS keychain (update for Big Sur)
#!/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 (ASCII character 9)
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