
下面的腳本可以從普通用戶執行,但我無法在 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