Skip to content

Commit 4bd013e

Browse files
committed
Fix displayed ssh port
1 parent 6bb3c95 commit 4bd013e

9 files changed

+73
-103
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ Replaces the `#H` format and adds a `#U` format option.
66

77
### Usage
88

9-
- `#H` will be the hostname of your current path. If there is an ssh session opened, the ssh hostname will show instead of the local one.
9+
- `#H` (`#{hostname}`) will be the hostname of your current path. If there is an ssh session opened, the ssh hostname will show instead of the local one.
1010
- `#{hostname_short}` will be the short hostname of your current path (up to the first dot). If there is an ssh session opened, the ssh hostname will show instead of the local one.
11-
- `#U` will show the `whoami` result or the user that logged in an ssh session.
11+
- `#U` (`#{username}`) will show the `whoami` result or the user that logged in an ssh session.
1212
- `#{pane_ssh_port}` if an open ssh session will show the connection port, otherwise it will be empty.
1313
- `#{pane_ssh_connected}` will be set to 1 if the currently selected pane has an active ssh connection. (Useful for `#{?#{pane_ssh_connected},ssh,no-ssh}` which will evaluate to `ssh` if there is an active ssh in the currently selected pane and `no-ssh` otherwise.)
14+
- `#{pane_ssh_connect}` if an open ssh session will show the connection info in `"username@hostname:port"` format, otherwise it will be empty.
1415

1516
Here's the example in `.tmux.conf`:
1617

current_pane_hostname.tmux

+20-11
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@
22

33
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
44

5-
interpolation=('\#H' '\#{hostname_short}' '\#U' '\#\{pane_ssh_port\}' '\#\{pane_ssh_connected\}')
6-
script=("#($CURRENT_DIR/scripts/hostname.sh)" "#($CURRENT_DIR/scripts/hostname_short.sh)" "#($CURRENT_DIR/scripts/whoami.sh)" "#($CURRENT_DIR/scripts/port.sh)" "#($CURRENT_DIR/scripts/pane_ssh_connected.sh)")
5+
# Create map placeholders to handle scripts
6+
# to provide one handler for multiple placeholders.
7+
# Use '//' as separator, due to unix limitation in filenames
8+
placeholders_to_scripts=(
9+
"\#U//#($CURRENT_DIR/scripts/user.sh)"
10+
"\#\{username\}//#($CURRENT_DIR/scripts/user.sh)"
11+
"\#H//#($CURRENT_DIR/scripts/host.sh)"
12+
"\#\{hostname\}//#($CURRENT_DIR/scripts/host.sh)"
13+
"\#\{hostname_short\}//#($CURRENT_DIR/scripts/host_short.sh)"
14+
"\#\{pane_ssh_port\}//#($CURRENT_DIR/scripts/port.sh)"
15+
"\#\{pane_ssh_connected\}//#($CURRENT_DIR/scripts/pane_ssh_connected.sh)"
16+
"\#\{pane_ssh_connect\}//#($CURRENT_DIR/scripts/pane_ssh_connect.sh)")
717

818

919
source $CURRENT_DIR/scripts/shared.sh
1020

1121
do_interpolation() {
12-
local interpolated=$1
13-
local j=0
14-
15-
for i in "${interpolation[@]}"; do
16-
local s=${script[$j]}
17-
local interpolated=${interpolated//$i/$s}
18-
((j+=1))
19-
done
20-
echo "$interpolated"
22+
local interpolated=$1
23+
for assignment in ${placeholders_to_scripts[@]}; do
24+
# ${assignment%\/\/*} - remove from // til EOL
25+
# ${assignment#*\/\/} - remove from BOL til //
26+
# ${interpolated//A/B} - replace all occurrences of A in interpolated with B
27+
local interpolated=${interpolated//${assignment%\/\/*}/${assignment#*\/\/}}
28+
done
29+
echo "$interpolated"
2130
}
2231

2332
update_tmux_option() {

scripts/whoami.sh scripts/host.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55
source $CURRENT_DIR/shared.sh
66

77
main() {
8-
get_info "whoami"
8+
get_info host
99
}
1010

1111
main

scripts/hostname.sh scripts/host_short.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55
source $CURRENT_DIR/shared.sh
66

77
main() {
8-
get_info "hostname"
8+
get_info host_short
99
}
1010

1111
main

scripts/hostname_short.sh scripts/pane_ssh_connect.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55
source $CURRENT_DIR/shared.sh
66

77
main() {
8-
get_info "hostname -s"
8+
get_info
99
}
1010

1111
main

scripts/pane_ssh_connected.sh

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55
source $CURRENT_DIR/shared.sh
66

77
main() {
8-
if ssh_connected; then
9-
echo 1
10-
else
11-
echo 0
12-
fi
8+
ssh_connected && echo 1 || echo 0
139
}
1410

1511
main

scripts/port.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
55
source $CURRENT_DIR/shared.sh
66

77
main() {
8-
if ssh_connected; then
9-
get_info "port"
10-
fi
8+
get_info port
119
}
1210

1311
main

scripts/shared.sh

+34-79
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ get_tmux_option() {
44
local option=$1
55
local default_value=$2
66
local option_value=$(tmux show-option -gqv "$option")
7-
if [ -z "$option_value" ]; then
8-
echo "$default_value"
9-
else
10-
echo "$option_value"
11-
fi
7+
[[ -z "$option_value" ]] && echo "$default_value" || echo "$option_value"
128
}
139

1410
set_tmux_option() {
@@ -17,87 +13,46 @@ set_tmux_option() {
1713
tmux set-option -gq "$option" "$value"
1814
}
1915

20-
parse_ssh_port() {
21-
# If there is a port get it
22-
local port=$(echo $1|grep -Eo '\-p\s*([0-9]+)'|sed 's/-p\s*//')
23-
24-
if [ -z $port ]; then
25-
local port=22
26-
fi
27-
28-
echo $port
29-
}
30-
31-
get_ssh_user() {
32-
local ssh_user=$(whoami)
33-
34-
for ssh_config in `awk '
35-
$1 == "Host" {
36-
gsub("\\\\.", "\\\\.", $2);
37-
gsub("\\\\*", ".*", $2);
38-
host = $2;
39-
next;
40-
}
41-
$1 == "User" {
42-
$1 = "";
43-
sub( /^[[:space:]]*/, "" );
44-
printf "%s|%s\n", host, $0;
45-
}' .ssh/config`; do
46-
local host_regex=${ssh_config%|*}
47-
local host_user=${ssh_config#*|}
48-
if [[ "$1" =~ $host_regex ]]; then
49-
ssh_user=$host_user
50-
break
51-
fi
52-
done
53-
54-
echo $ssh_user
55-
}
56-
5716
get_remote_info() {
58-
local command=$1
59-
60-
# First get the current pane command pid to get the full command with arguments
61-
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 //')
62-
63-
local port=$(parse_ssh_port "$cmd")
64-
65-
local cmd=$(echo $cmd|sed 's/\-p\s*'"$port"'//g')
66-
local user=$(echo $cmd | awk '{print $NF}'|cut -f1 -d@)
67-
local host=$(echo $cmd | awk '{print $NF}'|cut -f2 -d@)
17+
local command=$1
18+
local pane_pid=$(tmux display-message -p "#{pane_pid}")
6819

69-
if [ $user == $host ]; then
70-
local user=$(get_ssh_user $host)
71-
fi
72-
73-
case "$1" in
74-
"whoami")
75-
echo $user
76-
;;
77-
"hostname")
78-
echo $host
79-
;;
80-
"port")
81-
echo $port
82-
;;
83-
*)
84-
echo "$user@$host:$port"
85-
;;
86-
esac
20+
# First get the current pane command pid to get the full command with arguments
21+
local cmd=$({ pgrep -flaP $pane_pid ; ps -o command -p $pane_pid ; } | xargs -I{} echo {} | grep ssh | sed -E 's/^[0-9]*[[:blank:]]*ssh //')
22+
# Fetch configuration with given cmd
23+
ssh -G $cmd 2>/dev/null | grep -E -e '^host\s' -e '^port\s' -e '^user\s' | sort | cut -f 2 -d ' ' | xargs
8724
}
8825

8926
get_info() {
90-
# If command is ssh do some magic
91-
if ssh_connected; then
92-
echo $(get_remote_info $1)
93-
else
94-
echo $($1)
95-
fi
27+
# If command is ssh, fetch connection info
28+
if ssh_connected; then
29+
read -r host port user <<<$(get_remote_info)
30+
fi
31+
# Return requested info
32+
case "$1" in
33+
"user") # user from ssh info or `whoami`
34+
[[ -z "$user" ]] && whoami || echo "$user"
35+
;;
36+
"host") # host from ssh info or `hostname`
37+
[[ -z "$host" ]] && hostname || echo "$host"
38+
;;
39+
"host_short") # host from ssh info or `hostname -s`
40+
[[ -z "$host" ]] && hostname -s || echo "$host"
41+
;;
42+
"port") # port from ssh info or empty
43+
echo "$port"
44+
;;
45+
*) # user from ssh info + "@host" if host is not empty + ":port" if port is not empty
46+
echo "${user}${host:+@$host}${port:+:$port}"
47+
;;
48+
esac
9649
}
9750

9851
ssh_connected() {
99-
# Get current pane command
100-
local cmd=$(tmux display-message -p "#{pane_current_command}")
52+
local pane_pid=$(tmux display-message -p "#{pane_pid}")
53+
54+
# Get current pane command
55+
local cmd=$(pgrep -flaP $pane_pid)
10156

102-
[ $cmd = "ssh" ] || [ $cmd = "sshpass" ]
57+
[[ $cmd =~ " ssh " ]] || [[ $cmd =~ " sshpass " ]]
10358
}

scripts/user.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
5+
source $CURRENT_DIR/shared.sh
6+
7+
main() {
8+
get_info user
9+
}
10+
11+
main

0 commit comments

Comments
 (0)