下面的脚本可以从普通用户执行,但我无法在 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