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