Merge pull request #235 from AbelAnaya/master

Add ssh-session plugin
This commit is contained in:
Ethan Edwards 2023-12-03 18:35:41 -05:00 committed by GitHub
commit d8b7d01b72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 137 additions and 2 deletions

View file

@ -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. The order that you define the plugins will be the order on the status bar left to right.
```bash ```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" set -g @dracula-plugins "cpu-usage gpu-usage ram-usage"
``` ```
@ -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-server "google.com"
set -g @dracula-ping-rate 5 set -g @dracula-ping-rate 5
``` ```
### ssh-session options
Show SSH session port
```bash
set -g @dracula-show-ssh-session-port true
```
#### time options #### time options

View file

@ -18,6 +18,7 @@ Configuration and options can be found at [draculatheme.com/tmux](https://dracul
- Day, date, time, timezone - Day, date, time, timezone
- Current location based on network with temperature and forecast icon (if available) - Current location based on network with temperature and forecast icon (if available)
- Network connection status, bandwidth and SSID - Network connection status, bandwidth and SSID
- SSH session user, hostname and port of active tmux pane
- Git branch and status - Git branch and status
- Battery percentage and AC power connection status - Battery percentage and AC power connection status
- Refresh rate control - Refresh rate control

View file

@ -30,6 +30,7 @@ main()
show_refresh=$(get_tmux_option "@dracula-refresh-rate" 5) show_refresh=$(get_tmux_option "@dracula-refresh-rate" 5)
show_synchronize_panes_label=$(get_tmux_option "@dracula-synchronize-panes-label" "Sync") show_synchronize_panes_label=$(get_tmux_option "@dracula-synchronize-panes-label" "Sync")
time_format=$(get_tmux_option "@dracula-time-format" "") 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") IFS=' ' read -r -a plugins <<< $(get_tmux_option "@dracula-plugins" "battery network weather")
show_empty_plugins=$(get_tmux_option "@dracula-show-empty-plugins" true) show_empty_plugins=$(get_tmux_option "@dracula-show-empty-plugins" true)
@ -257,6 +258,11 @@ main()
elif [ $plugin = "synchronize-panes" ]; then elif [ $plugin = "synchronize-panes" ]; then
IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-synchronize-panes-colors" "cyan dark_gray") 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)" 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 else
continue continue
fi fi

122
scripts/ssh_session.sh Executable file
View file

@ -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