From 1ef9b68d80da43d85b394c64247ce47e88b75e8d Mon Sep 17 00:00:00 2001 From: Gregory Pakosz Date: Wed, 20 Oct 2021 22:37:03 +0200 Subject: [PATCH 1/3] improved _pane_info() accuracy by excluding ssh proxy connections, tee, and only considering the first child of a process ps output is sorted by controlling terminal then pid in case of a cmd1 | cmd2 | cmd3 pipe chain, we only consider the first child of the parent process, which is cmd1 in the majority of cases the rare situation when pids wrap around isn't worth additional work on Linux - we sort ps output by lstart in hope precision is enough to make cmd1 always appear first even when pids wrap around - the more complex solution involves inspecting /proc//fd/0 to filter out processes being piped to finally, while lsof was promising, it won't list processes owned by other users unless run as root --- .tmux.conf | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/.tmux.conf b/.tmux.conf index 3e3bdca..48f0117 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -501,8 +501,8 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # case "$_uname_s" in # *CYGWIN*) # ps -al | tail -n +2 | awk -v pane_pid="$pane_pid" -v tty="$pane_tty" ' -# ((/ssh/ && !/-W/) || !/ssh/) && $5 == tty { -# user[$1] = $6; parent[$1] = $2; child[$2] = $1 +# ((/ssh/ && !/-W/) || !/ssh/) && !/tee/ && $5 == tty { +# user[$1] = $6; if (!child[$2]) child[$2] = $1 # } # END { # pid = pane_pid @@ -516,10 +516,24 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # } # ' # ;; +# *Linux*) +# ps -t "$pane_tty" --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command= | awk -v pane_pid="$pane_pid" ' +# ((/ssh/ && !/-W/) || !/ssh/) && !/tee/ { +# user[$2] = $1; if (!child[$3]) child[$3] = $2; pid=$2; $1 = $2 = $3 = ""; command[pid] = substr($0,4) +# } +# END { +# pid = pane_pid +# while (child[pid]) +# pid = child[pid] +# +# print pid":"user[pid]":"command[pid] +# } +# ' +# ;; # *) # ps -t "$pane_tty" -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command= | awk -v pane_pid="$pane_pid" ' -# NR > 1 && ((/ssh/ && !/-W/) || !/ssh/) { -# user[$2] = $1; parent[$2] = $3; child[$3] = $2; pid=$2; $1 = $2 = $3 = ""; command[pid] = substr($0,4) +# ((/ssh/ && !/-W/) || !/ssh/) && !/tee/ { +# user[$2] = $1; if (!child[$3]) child[$3] = $2; pid=$2; $1 = $2 = $3 = ""; command[pid] = substr($0,4) # } # END { # pid = pane_pid From c6bab99f051c73c5ba3879751f25bb66c4c6be0d Mon Sep 17 00:00:00 2001 From: Gregory Pakosz Date: Tue, 2 Nov 2021 14:21:55 +0100 Subject: [PATCH 2/3] updated _ssh_or_mosh_args() relaxed the regex to accommodate for renamed ssh clients and to ignore the command if supplied --- .tmux.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.tmux.conf b/.tmux.conf index 48f0117..5a6b808 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -550,7 +550,7 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # _ssh_or_mosh_args() { # case "$1" in # *ssh*) -# args=$(printf '%s' "$1" | perl -n -e 'print if s/(.*?)\bssh\b\s+(.*)/\2/') +# args=$(printf '%s' "$1" | perl -n -e 'print if s/.*?\bssh[\w]*\s*((?:\s+-\w+)*)(\s+\w+)(\s\w+)?/\1\2/') # ;; # *mosh-client*) # args=$(printf '%s' "$1" | sed -E -e 's/.*mosh-client -# (.*)\|.*$/\1/' -e 's/-[^ ]*//g' -e 's/\d:\d//g') From d471ccc068d83670b5b21be27eb2d33cc9f4f417 Mon Sep 17 00:00:00 2001 From: Gregory Pakosz Date: Wed, 10 Nov 2021 19:37:03 +0100 Subject: [PATCH 3/3] updated _username() in when the ssh client doesn't support %r in ProxyCommand in such a case, we use -o IdentityFile='%%username%%/%r' and parse the output of ssh -v --- .tmux.conf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.tmux.conf b/.tmux.conf index 5a6b808..62447e5 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -574,7 +574,8 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # # shellcheck disable=SC2086 # username=$(ssh -G $ssh_or_mosh_args 2>/dev/null | awk '/^user / { print $2; exit }') # # shellcheck disable=SC2086 -# [ -z "$username" ] && username=$(ssh -T -o ControlPath=none -o ProxyCommand="sh -c 'echo %%username%% %r >&2'" $ssh_or_mosh_args 2>&1 | awk '/^%username% / { print $2; exit }') +# [ -z "$username" ] && username=$(ssh $ssh_or_mosh_args -T -o ControlPath=none -o ProxyCommand="sh -c 'echo %%username%% %r >&2'" 2>&1 | awk '/^%username% / { print $2; exit }') +# [ -z "$username" ] && username=$(ssh $ssh_or_mosh_args -v -T -o ControlPath=none -o ProxyCommand=false -o IdentityFile='%%username%%/%r' 2>&1 | awk '/%username%/ { print substr($4,12); exit }') # else # if ! _is_enabled "$ssh_only"; then # username=${pane_info#*:}