how to make scutil login to VPN with password on Mac?

I wrote a shell script that fetches the password from keychain and then pastes it into the popup. You have to make a keychain item with the password for this to work.

# VPN.sh 
# change these variables for your setup

keychainItem=accountWithPassword      # this name has to match "Account" for the entry you make in keychain
VPNName="VPN (Cisco IPSec)"   # match the name of the VPN service to run

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"

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

exit

Tags:

Macos

Vpn