From cbea4aeb586cecbd5574910dde445f4e326a7d1a Mon Sep 17 00:00:00 2001 From: Tommy Andersson Date: Mon, 10 Feb 2025 23:45:21 +0100 Subject: [PATCH] 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. --- README.rst | 13 +++++++++++++ auto-notify.plugin.zsh | 27 +++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index a69648d..6318ced 100644 --- a/README.rst +++ b/README.rst @@ -129,6 +129,19 @@ NOTE: This configuration option currently only works for Linux. # Set notification expiry to 10 seconds 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** diff --git a/auto-notify.plugin.zsh b/auto-notify.plugin.zsh index 591a47e..be8b1bb 100644 --- a/auto-notify.plugin.zsh +++ b/auto-notify.plugin.zsh @@ -6,6 +6,9 @@ export AUTO_NOTIFY_VERSION="0.10.2" # Threshold in seconds for when to automatically show a notification [[ -z "$AUTO_NOTIFY_THRESHOLD" ]] && 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 [[ -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") - if [[ -n "$icon" ]]; then - arguments+=("--icon=$icon") - fi - notify-send ${arguments[@]} + if [[ -n "$icon" ]]; then + arguments+=("--icon=$icon") + fi + + # 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 osascript \ @@ -90,8 +105,8 @@ function _is_auto_notify_ignored() { # Remove leading whitespace target_command="$(echo "$target_command" | sed -e 's/^ *//')" - # If the command is being run over SSH, then ignore it - if [[ -n ${SSH_CLIENT-} || -n ${SSH_TTY-} || -n ${SSH_CONNECTION-} ]]; then + # Ignore the command if running over SSH and AUTO_NOTIFY_ENABLE_SSH is disabled + if [[ -n ${SSH_CLIENT-} || -n ${SSH_TTY-} || -n ${SSH_CONNECTION-} ]] && [[ "${AUTO_NOTIFY_ENABLE_SSH-1}" == "0" ]]; then print "yes" return fi