Add option to enable forwarding of notifications over SSH

Option AUTO_NOTIFY_ENABLE_SSH has been added to
enable and disable forwarding of notifications to
local client for long running commands issued over
SSH on remote servers.
This commit is contained in:
Tommy Andersson 2025-02-10 23:45:21 +01:00
parent 76a527a66b
commit cbea4aeb58
2 changed files with 34 additions and 6 deletions

View File

@ -129,6 +129,19 @@ NOTE: This configuration option currently only works for Linux.
# Set notification expiry to 10 seconds # Set notification expiry to 10 seconds
export AUTO_NOTIFY_EXPIRE_TIME=10000 export AUTO_NOTIFY_EXPIRE_TIME=10000
**Notification Forwarding Over SSH**
You can configure whether notifications will be forwarded to SSH client by setting the environment
variable ``AUTO_NOTIFY_ENABLE_SSH`` to enable ("1") or disable ("0"). The default value is set to 0.
NOTE: This configuration option currently only works for Linux.
::
# Enable notification forwarding to SSH client
export AUTO_NOTIFY_ENABLE_SSH=1
# Disable notification forwarding to SSH client
export AUTO_NOTIFY_ENABLE_SSH=0
**Ignored Commands** **Ignored Commands**

View File

@ -6,6 +6,9 @@ export AUTO_NOTIFY_VERSION="0.10.2"
# Threshold in seconds for when to automatically show a notification # Threshold in seconds for when to automatically show a notification
[[ -z "$AUTO_NOTIFY_THRESHOLD" ]] && [[ -z "$AUTO_NOTIFY_THRESHOLD" ]] &&
export AUTO_NOTIFY_THRESHOLD=10 export AUTO_NOTIFY_THRESHOLD=10
# Enable or disable notifications for SSH sessions (0 = disabled, 1 = enabled)
[[ -z "$AUTO_NOTIFY_ENABLE_SSH" ]] &&
export AUTO_NOTIFY_ENABLE_SSH=0
# List of commands/programs to ignore sending notifications for # List of commands/programs to ignore sending notifications for
[[ -z "$AUTO_NOTIFY_IGNORE" ]] && [[ -z "$AUTO_NOTIFY_IGNORE" ]] &&
@ -65,10 +68,22 @@ function _auto_notify_message() {
local arguments=("$title" "$body" "--app-name=zsh" "$transient" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME") local arguments=("$title" "$body" "--app-name=zsh" "$transient" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME")
if [[ -n "$icon" ]]; then if [[ -n "$icon" ]]; then
arguments+=("--icon=$icon") arguments+=("--icon=$icon")
fi fi
notify-send ${arguments[@]}
# Check if the script is running over SSH
if [[ -n "${SSH_CLIENT}" || -n "${SSH_CONNECTION}" ]]; then
# Extract the client IP address from environment
local client_ip="${SSH_CLIENT%% *}"
[[ -z "$client_ip" ]] && client_ip="${SSH_CONNECTION%% *}"
# Forward the notify-send command to the client machine via SSH
ssh "${USER}@${client_ip}" "$(printf '%q ' notify-send "${arguments[@]}")"
else
# If not running over SSH, send notification locally
notify-send "${arguments[@]}"
fi
elif [[ "$platform" == "Darwin" ]]; then elif [[ "$platform" == "Darwin" ]]; then
osascript \ osascript \
@ -90,8 +105,8 @@ function _is_auto_notify_ignored() {
# Remove leading whitespace # Remove leading whitespace
target_command="$(echo "$target_command" | sed -e 's/^ *//')" target_command="$(echo "$target_command" | sed -e 's/^ *//')"
# If the command is being run over SSH, then ignore it # Ignore the command if running over SSH and AUTO_NOTIFY_ENABLE_SSH is disabled
if [[ -n ${SSH_CLIENT-} || -n ${SSH_TTY-} || -n ${SSH_CONNECTION-} ]]; then if [[ -n ${SSH_CLIENT-} || -n ${SSH_TTY-} || -n ${SSH_CONNECTION-} ]] && [[ "${AUTO_NOTIFY_ENABLE_SSH-1}" == "0" ]]; then
print "yes" print "yes"
return return
fi fi