从bash脚本中触发通知

从bash脚本中触发通知,bash,macos,launchd,osascript,launch-daemon,Bash,Macos,Launchd,Osascript,Launch Daemon,目标设备:macOS Catalina及更高版本 我可能需要一些帮助来修复脚本中的问题,该脚本应该在用户尝试连接到被禁止的SSID时触发osascript通知。只有当用户已经连接或试图连接到其中一个被禁止的SSID时,才会发出通知 我认为问题是由于脚本是由launchd运行的,因此是以root用户身份运行的,但是,即使以登录用户身份运行通知命令,也不会发生任何通知,即使脚本的其余部分工作正常 其次,我们也无法从local items密钥链中删除禁用SSID的凭据,但照目前的情况,该脚本具有理想的

目标设备:macOS Catalina及更高版本

我可能需要一些帮助来修复脚本中的问题,该脚本应该在用户尝试连接到被禁止的SSID时触发osascript通知。只有当用户已经连接或试图连接到其中一个被禁止的SSID时,才会发出通知

我认为问题是由于脚本是由launchd运行的,因此是以root用户身份运行的,但是,即使以登录用户身份运行通知命令,也不会发生任何通知,即使脚本的其余部分工作正常

其次,我们也无法从local items密钥链中删除禁用SSID的凭据,但照目前的情况,该脚本具有理想的效果,可以在连接时将机器从禁用网络中踢出,并防止机器将来自动连接。我们可以从系统密钥链中删除凭证,但最好也能找到一种从本地项密钥链中删除该项的方法

无论如何,主要问题发生在下面修改代码的第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```

那么,您遇到的来自守护进程的通知的问题就是设计上的问题

它与macOS如何在不同会话中运行有关,您可以阅读和了解更多信息

现在您需要知道的是,当作为守护进程运行时,即使使用sudo-u,您也无法默认访问用户GUI会话

但是,正如前面所描述的,有一些方法可以从上下文访问用户GUI会话

总而言之,您需要做的是:

  • 改变
  • sudo-u$loggedInUser osascript-e…

    sudo launchctl asuser$userId osascript-e…

    其中$userId是这样的:

    userId=`sudo -u $USER id -u`
    
    (我不太喜欢bash,可以用更清晰的方式完成)

  • 检查sh是否在安全首选项内被授予完全磁盘访问权限(或者沙盒配置文件不允许您读取sh脚本)

  • 谢谢,这正是我需要的。测试和工作完美无瑕。