Skip to content

Commit

Permalink
Merge pull request #235 from AbelAnaya/master
Browse files Browse the repository at this point in the history
Add ssh-session plugin
  • Loading branch information
ethancedwards8 authored Dec 3, 2023
2 parents 1b328c1 + 892e659 commit d8b7d01
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
10 changes: 8 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +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, mpc, 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, mpc, spotify-tui, kubernetes-context, synchronize-panes
set -g @dracula-plugins "cpu-usage gpu-usage ram-usage"
```

Expand Down Expand Up @@ -189,6 +188,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

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions scripts/dracula.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -257,6 +258,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
Expand Down
122 changes: 122 additions & 0 deletions scripts/ssh_session.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/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
}

parse_ssh_config() {
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;
}' $1`; do
local host_regex=${ssh_config%|*}
local host_user=${ssh_config#*|}
if [ "$2" == "$host_regex" ]; then
ssh_user_found=$host_user
break
fi
done

echo $ssh_user_found
}

get_ssh_user() {
# Search SSH User in user local file if available
if [ -f ~/.ssh/config ]; then
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
}

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

0 comments on commit d8b7d01

Please sign in to comment.