From c9c08f58e0fac97b33059ea8dff9fa29793b45b1 Mon Sep 17 00:00:00 2001 From: AbelAnaya Date: Thu, 12 Oct 2023 00:10:35 +0200 Subject: [PATCH 1/7] feat: Add ssh-session plugin Shows user and hostname of active ssh session if any in the current tmux pane. Connected port is also available to show under configuration option (default false) --- scripts/dracula.sh | 8 +++- scripts/ssh_session.sh | 105 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100755 scripts/ssh_session.sh diff --git a/scripts/dracula.sh b/scripts/dracula.sh index 0248ded..2e8bbd7 100755 --- a/scripts/dracula.sh +++ b/scripts/dracula.sh @@ -30,6 +30,7 @@ main() show_refresh=$(get_tmux_option "@dracula-refresh-rate" 5) show_synchronize_panes_label=$(get_tmux_option "@dracula-synchronize-panes-label" "Sync") time_format=$(get_tmux_option "@dracula-time-format" "") + show_ssh_session_port=$(get_tmux_option "@dracula-show-ssh-session-port" false) IFS=' ' read -r -a plugins <<< $(get_tmux_option "@dracula-plugins" "battery network weather") show_empty_plugins=$(get_tmux_option "@dracula-show-empty-plugins" true) @@ -146,7 +147,7 @@ main() IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-cwd-colors" "dark_gray white") tmux set-option -g status-right-length 250 script="#($current_dir/cwd.sh)" - + elif [ $plugin = "fossil" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-fossil-colors" "green dark_gray") tmux set-option -g status-right-length 250 @@ -249,6 +250,11 @@ main() elif [ $plugin = "synchronize-panes" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-synchronize-panes-colors" "cyan dark_gray") script="#($current_dir/synchronize_panes.sh $show_synchronize_panes_label)" + + elif [ $plugin = "ssh-session" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-ssh-session-colors" "green dark_gray") + script="#($current_dir/ssh_session.sh $show_ssh_session_port)" + else continue fi diff --git a/scripts/ssh_session.sh b/scripts/ssh_session.sh new file mode 100755 index 0000000..6f2a8c3 --- /dev/null +++ b/scripts/ssh_session.sh @@ -0,0 +1,105 @@ +#!/usr/bin/env bash + +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +show_ssh_session_port=$1 + +parse_ssh_port() { + # Get port from connection + local port=$(echo $1|grep -Eo '\-p\s*([0-9]+)'|sed 's/-p\s*//') + + if [ -z $port ]; then + local port=22 + fi + + echo $port +} + +get_ssh_user() { + local ssh_user=$(whoami) + + for ssh_config in `awk ' + $1 == "Host" { + gsub("\\\\.", "\\\\.", $2); + gsub("\\\\*", ".*", $2); + host = $2; + next; + } + $1 == "User" { + $1 = ""; + sub( /^[[:space:]]*/, "" ); + printf "%s|%s\n", host, $0; + }' .ssh/config`; do + local host_regex=${ssh_config%|*} + local host_user=${ssh_config#*|} + if [[ "$1" =~ $host_regex ]]; then + ssh_user=$host_user + break + fi + done + + echo $ssh_user +} + +get_remote_info() { + local command=$1 + + # First get the current pane command pid to get the full command with arguments + local cmd=$({ pgrep -flaP `tmux display-message -p "#{pane_pid}"` ; ps -o command -p `tmux display-message -p "#{pane_pid}"` ; } | xargs -I{} echo {} | grep ssh | sed -E 's/^[0-9]*[[:blank:]]*ssh //') + local port=$(parse_ssh_port "$cmd") + + local cmd=$(echo $cmd|sed 's/\-p\s*'"$port"'//g') + local user=$(echo $cmd | awk '{print $NF}'|cut -f1 -d@) + local host=$(echo $cmd | awk '{print $NF}'|cut -f2 -d@) + + if [ $user == $host ]; then + local user=$(get_ssh_user $host) + fi + + case "$1" in + "whoami") + echo $user + ;; + "hostname") + echo $host + ;; + "port") + echo $port + ;; + *) + echo "$user@$host:$port" + ;; + esac +} + +get_info() { + # If command is ssh get info from remote + if $(ssh_connected); then + echo $(get_remote_info $1) + else + echo $($1) + fi +} + +ssh_connected() { + # Get current pane command + local cmd=$(tmux display-message -p "#{pane_current_command}") + + [ $cmd = "ssh" ] || [ $cmd = "sshpass" ] +} + +main() { + hostname=$(get_info hostname) + user=$(get_info whoami) + + # Only show port info if ssh session connected (no localhost) and option enabled + if $(ssh_connected) && [[ $show_ssh_session_port == "true" ]] ; then + port=$(get_info port) + echo $user@$hostname:$port + else + echo $user@$hostname + fi +} + +main From bda4fdaf21c2510ddbaf64edca444c5dff48802d Mon Sep 17 00:00:00 2001 From: AbelAnaya Date: Thu, 12 Oct 2023 00:10:36 +0200 Subject: [PATCH 2/7] docs: Add documentation for ssh-session plugin --- INSTALL.md | 9 ++++++++- README.md | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/INSTALL.md b/INSTALL.md index 8e04980..f85601d 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -48,7 +48,7 @@ To enable plugins set up the `@dracula-plugins` option in you `.tmux.conf` file, The order that you define the plugins will be the order on the status bar left to right. ```bash -# available plugins: battery, cpu-usage, git, gpu-usage, ram-usage, tmux-ram-usage, network, network-bandwidth, network-ping, attached-clients, network-vpn, weather, time, spotify-tui, kubernetes-context, synchronize-panes +# available plugins: battery, cpu-usage, git, gpu-usage, ram-usage, tmux-ram-usage, network, network-bandwidth, network-ping, ssh-session, attached-clients, network-vpn, weather, time, spotify-tui, kubernetes-context, synchronize-panes set -g @dracula-plugins "cpu-usage gpu-usage ram-usage" ``` @@ -189,6 +189,13 @@ You can configure which server (hostname, IP) you want to ping and at which rate set -g @dracula-ping-server "google.com" set -g @dracula-ping-rate 5 ``` +### ssh-session options + +Show SSH session port + +```bash +set -g @dracula-show-ssh-session-port true +``` #### time options diff --git a/README.md b/README.md index 6fcd9ef..217aa2f 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Configuration and options can be found at [draculatheme.com/tmux](https://dracul - Day, date, time, timezone - Current location based on network with temperature and forecast icon (if available) - Network connection status, bandwidth and SSID +- SSH session user, hostname and port of active tmux pane - Git branch and status - Battery percentage and AC power connection status - Refresh rate control From ac4639be61984b84d23c9686db704e6685402ba9 Mon Sep 17 00:00:00 2001 From: AbelAnaya Date: Mon, 6 Nov 2023 22:21:57 +0100 Subject: [PATCH 3/7] fix: Use correct path for ssh configuration file --- scripts/ssh_session.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ssh_session.sh b/scripts/ssh_session.sh index 6f2a8c3..7d106d1 100755 --- a/scripts/ssh_session.sh +++ b/scripts/ssh_session.sh @@ -30,7 +30,7 @@ get_ssh_user() { $1 = ""; sub( /^[[:space:]]*/, "" ); printf "%s|%s\n", host, $0; - }' .ssh/config`; do + }' /etc/ssh/ssh_config`; do local host_regex=${ssh_config%|*} local host_user=${ssh_config#*|} if [[ "$1" =~ $host_regex ]]; then From ffa8d28142560ec703880ccacc9feaead31646e2 Mon Sep 17 00:00:00 2001 From: AbelAnaya Date: Tue, 7 Nov 2023 18:43:49 +0100 Subject: [PATCH 4/7] refactor: Check both /etc/ssh/ssh_config and ~/.ssh/config to retrieve user under host --- scripts/ssh_session.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/scripts/ssh_session.sh b/scripts/ssh_session.sh index 7d106d1..3673dd4 100755 --- a/scripts/ssh_session.sh +++ b/scripts/ssh_session.sh @@ -39,6 +39,26 @@ get_ssh_user() { fi done + for ssh_config in `awk ' + $1 == "Host" { + gsub("\\\\.", "\\\\.", $2); + gsub("\\\\*", ".*", $2); + host = $2; + next; + } + $1 == "User" { + $1 = ""; + sub( /^[[:space:]]*/, "" ); + printf "%s|%s\n", host, $0; + }' .ssh/config`; do + local host_regex=${ssh_config%|*} + local host_user=${ssh_config#*|} + if [[ "$1" =~ $host_regex ]]; then + ssh_user=$host_user + break + fi + done + echo $ssh_user } From 31c8af34460b10b3ddeb060dde075a66909433a9 Mon Sep 17 00:00:00 2001 From: AbelAnaya Date: Mon, 13 Nov 2023 23:54:56 +0100 Subject: [PATCH 5/7] refactor: Remove code duplication and check local config file exists before parsing --- scripts/ssh_session.sh | 69 +++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/scripts/ssh_session.sh b/scripts/ssh_session.sh index 3673dd4..b6593ae 100755 --- a/scripts/ssh_session.sh +++ b/scripts/ssh_session.sh @@ -16,48 +16,41 @@ parse_ssh_port() { echo $port } +search_ssh_user() { + for ssh_config in `awk ' + $2 == "Host" { + gsub("\\\\.", "\\\\.", $3); + gsub("\\\\*", ".*", $3); + host = $3; + next; + } + $2 == "User" { + $2 = ""; + sub( /^[[:space:]]*/, "" ); + printf "%s|%s\n", host, $0; + }' $1`; do + local host_regex=${ssh_config%|*} + local host_user=${ssh_config#*|} + if [[ "$2" =~ $host_regex ]]; then + ssh_user=$host_user + break + fi + done + + echo $ssh_user +} + get_ssh_user() { + # Set default ssh_user as current user local ssh_user=$(whoami) - for ssh_config in `awk ' - $1 == "Host" { - gsub("\\\\.", "\\\\.", $2); - gsub("\\\\*", ".*", $2); - host = $2; - next; - } - $1 == "User" { - $1 = ""; - sub( /^[[:space:]]*/, "" ); - printf "%s|%s\n", host, $0; - }' /etc/ssh/ssh_config`; do - local host_regex=${ssh_config%|*} - local host_user=${ssh_config#*|} - if [[ "$1" =~ $host_regex ]]; then - ssh_user=$host_user - break - fi - done + # Search SSH User information in global configuration file + ssh_user=$(search_ssh_user /etc/ssh/ssh_config $1) - for ssh_config in `awk ' - $1 == "Host" { - gsub("\\\\.", "\\\\.", $2); - gsub("\\\\*", ".*", $2); - host = $2; - next; - } - $1 == "User" { - $1 = ""; - sub( /^[[:space:]]*/, "" ); - printf "%s|%s\n", host, $0; - }' .ssh/config`; do - local host_regex=${ssh_config%|*} - local host_user=${ssh_config#*|} - if [[ "$1" =~ $host_regex ]]; then - ssh_user=$host_user - break - fi - done + if [ -f ~/.ssh/config ]; then + # Search SSH User information in user configuration file + ssh_user=$(search_ssh_user ~/.ssh/config $1) + fi echo $ssh_user } From ad59ebcf3a40e23d81b52057590d0273c2a5bbd6 Mon Sep 17 00:00:00 2001 From: AbelAnaya Date: Tue, 14 Nov 2023 23:16:55 +0100 Subject: [PATCH 6/7] refactor: Improve script maintainability and readability with cleaner functions --- scripts/ssh_session.sh | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/scripts/ssh_session.sh b/scripts/ssh_session.sh index b6593ae..acc3614 100755 --- a/scripts/ssh_session.sh +++ b/scripts/ssh_session.sh @@ -16,40 +16,44 @@ parse_ssh_port() { echo $port } -search_ssh_user() { +parse_ssh_config() { for ssh_config in `awk ' - $2 == "Host" { - gsub("\\\\.", "\\\\.", $3); - gsub("\\\\*", ".*", $3); - host = $3; + $1 == "Host" { + gsub("\\\\.", "\\\\.", $2); + gsub("\\\\*", ".*", $2); + host = $2; next; } - $2 == "User" { - $2 = ""; + $1 == "User" { + $1 = ""; sub( /^[[:space:]]*/, "" ); printf "%s|%s\n", host, $0; }' $1`; do local host_regex=${ssh_config%|*} local host_user=${ssh_config#*|} - if [[ "$2" =~ $host_regex ]]; then - ssh_user=$host_user + if [[ "$2" == $host_regex ]]; then + ssh_user_found=$host_user break fi done - echo $ssh_user + echo $ssh_user_found } get_ssh_user() { - # Set default ssh_user as current user - local ssh_user=$(whoami) - - # Search SSH User information in global configuration file - ssh_user=$(search_ssh_user /etc/ssh/ssh_config $1) - + # Search SSH User in user local file if available if [ -f ~/.ssh/config ]; then - # Search SSH User information in user configuration file - ssh_user=$(search_ssh_user ~/.ssh/config $1) + ssh_user=$(parse_ssh_config ~/.ssh/config $1) + fi + + # If SSH User not found, search in global config file + if [ -z $ssh_user ]; then + ssh_user=$(parse_ssh_config /etc/ssh/ssh_config $1) + fi + + #If SSH User not found in any config file, return current user + if [ -z $ssh_user ]; then + ssh_user=$(whoami) fi echo $ssh_user From b908ce803b8575206e3a66097348f29b78f9c4e1 Mon Sep 17 00:00:00 2001 From: AbelAnaya Date: Tue, 14 Nov 2023 23:17:38 +0100 Subject: [PATCH 7/7] refactor: Avoid if statements with double quotes and use single quotes instead to improve portability of plugin --- scripts/ssh_session.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/ssh_session.sh b/scripts/ssh_session.sh index acc3614..92daba5 100755 --- a/scripts/ssh_session.sh +++ b/scripts/ssh_session.sh @@ -31,7 +31,7 @@ parse_ssh_config() { }' $1`; do local host_regex=${ssh_config%|*} local host_user=${ssh_config#*|} - if [[ "$2" == $host_regex ]]; then + if [ "$2" == "$host_regex" ]; then ssh_user_found=$host_user break fi @@ -111,7 +111,7 @@ main() { user=$(get_info whoami) # Only show port info if ssh session connected (no localhost) and option enabled - if $(ssh_connected) && [[ $show_ssh_session_port == "true" ]] ; then + if $(ssh_connected) && [ "$show_ssh_session_port" == "true" ] ; then port=$(get_info port) echo $user@$hostname:$port else