目标设备: macOS Catalina及更高版本
当用户试图连接到禁用的SSID时,我可能需要一些帮助来解决脚本中的一个问题,该脚本应该触发一个抄本通知。只有当用户已经连接或试图连接到禁用的SSID之一时,才会发出通知。
我认为,这个问题是因为脚本是由launchd运行的,因此也是以root的形式运行的,但是,即使在以登录用户的身份运行通知命令之后,即使脚本的其余部分运行良好,也不会出现通知。
其次,我们也无法从本地项目密钥链中删除禁用SSID的凭据,但实际上,如果连接到禁用网络,脚本就会产生希望的效果,并防止机器在将来自动连接。我们可以从系统密钥链中删除凭据,但最好也找到一种方法从Local键链中删除该项。
无论如何,主要问题发生在下面修改的代码的第47行。任何帮助解决这两个问题都将不胜感激。
对此片段进行了修改,以便更容易地识别违规命令:
#
# This script will find all saved SSIDs, compare them to a list of banned SSIDs and if found, removes them
#
# If the client is connected to a banned SSID, Wi-Fi is toggled to allow automatic connection to a non-banned SSID
#
# Script is only able to remove SSID from System keychain as delete-generic-password is not "Local Items" aware
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Change Internal Field Seperator to " " to allow for SSIDs that contain spaces in array "bannedNetworks"
IFS=' '
# Get current logged in user
loggedInUser=`ls -l /dev/console | cut -d " " -f 4`
# Determine the Wi-Fi interface
interface=$(networksetup -listallhardwareports | grep -E '(Wi-Fi|AirPort)' -A 1 | grep -o en.)
# Get all saved SSIDs
savedNetworks=($(networksetup -listpreferredwirelessnetworks $interface | tail -n +2))
# SSIDs to be removed
bannedNetworks=("SSIDone" "SSIDtwo" "SSIDthree")
# Power cycle wireless adapter if connected to a banned network, then remove it
for i in "${bannedNetworks[@]}"
do
if [[ $(networksetup -getairportnetwork $interface | cut -d ":" -f 2 | cut -c 2-) != $i ]]; then
echo "Not connected to $i"
else
networksetup -removepreferredwirelessnetwork $interface $i
sudo security delete-generic-password -l $i "/Library/Keychains/System.keychain" >/dev/null 2>&1
# Update savedNetworks variable to prevent "…not found" error as the connected network has already been removed yet remains in the array
savedNetworks=($(networksetup -listpreferredwirelessnetworks $interface | tail -n +2))
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Notify the user: Doesn't trigger properly, even when run as the logged in user
sudo -u $loggedInUser osascript -e 'display notification "The Wi-Fi network you selected is not for use with district devices. If \"ApprovedNetwork\" fails, please use \"BackupNetwork.\"" with title "Blocked Network"'
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
networksetup -setairportpower $interface off
sleep 5
networksetup -setairportpower $interface on
fi
done```发布于 2021-04-13 20:01:05
好吧,您所遇到的守护进程通知的问题在于设计。
这与macOS如何使用不同的会话有关,您可以阅读这里和这里获得更多信息。
现在您需要知道的是,在作为守护进程运行时,即使使用sudo -u,也没有对用户GUI会话的默认访问权限。
但是,有一些方法可以从上下文中访问用户GUI会话,如这里所描述的那样
总之,你需要做的是:
sudo -u $loggedInUser osascript -e ...
至
sudo launchctl asuser $userId osascript -e ...
其中$userId是这样的:
userId=`sudo -u $USER id -u`(我不太喜欢巴什,可以用一种更清晰的方式完成)
https://stackoverflow.com/questions/67079242
复制相似问题