
아래 스크립트는 일반 사용자에서 실행할 수 있지만 macos(bigsur)에서 cronjob을 통해 스크립트를 실행할 수 없습니다. 작업 보안을 통해 스크립트를 실행할 때 다음 오류가 발생합니다. SecKeychainSearchCopyNext: 지정된 항목을 키체인에서 찾을 수 없습니다.
#!/bin/sh
keychainItem="connectvpn"
# this name has to match "Account" for the entry you make in keychain
VPNName="DIS CVPN" # match the name of the VPN service to run
function isnt_connected () {
scutil --nc status "$VPNName" | sed -n 1p | grep -qv Connected
}
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"
if isnt_connected $VPNName; then
echo "Connecting to VPN..."
scutil --nc start "$VPNName"
sleep 1
osascript -e "tell application \"System Events\" to keystroke tab"
osascript -e "tell application \"System Events\" to keystroke \"$(get_pw)\""
osascript -e "tell application \"System Events\" to keystroke return"
else
echo "Already Connected to VPN..."
fi
내 crontab 라인은 다음과 같습니다.
*/1 * * * * /Users/demo/vpn/connect.sh >> /Users/demo/vpn/vpnlog.log 2>&1