gpakosz / .tmux

🇫🇷 Oh my tmux! My self-contained, pretty & versatile tmux configuration made with ❤️
MIT License
21.59k stars 3.33k forks source link

hostname_ssh not displaying in status bar #627

Closed TheZenTester closed 1 year ago

TheZenTester commented 1 year ago

Hello I'm trying to get the hostname_ssh variable to work. I'm assuming this means if I have my local system, called localsystem and I ssh into a remote system of jumpbox, it would display jumpbox in the bottom right hand corner when i'm ssh'd in, but localsystem when I am not.

I currently can get localsystem to display. However, when I ssh into the jumpbox, the display remains blank. I've tried this with both #{hostname_ssh} variables by itself, as well as the conditional variable I have set below.

Apologies if this is a rudimentary question, but it's been something I've tried digging into for a few hours but can't find clear guidance.

I'm also using zsh and oh-my-zsh.

tmux_conf_theme_status_right="#{prefix}#{pairing}#{synchronized} ,tun0:#(ip addr show dev tun0 | grep 'inet[^6]' | awk '{print $2}' | cut -d/ -f1),eth:#(ip addr show dev enx24f5a28cd271 | grep 'inet[^6]' | awk '{print $2}' | cut -d/ -f1), %b %d,%R | #{?my_hostname,#{hostname_ssh},#{hostname}}"
gpakosz commented 1 year ago

Hello @TheZenTester,

All you need is #{hostname}

Can you please, set tmux_conf_theme_status_right to only an try it?

tmux_conf_theme_status_right='#{hostname}'
TheZenTester commented 1 year ago

Apologies, I forgot to mention that solution doesn't work. When just set to #{hostname}, the status bar is blank. Even when following your instructions to set tmux_conf_theme_status_right='#{hostname}'

gpakosz commented 1 year ago

Ok then let's try to troubleshoot this

  1. What's your OS?

  2. Then set tmux_conf_theme_status_right to #{hostname}

    tmux_conf_theme_status_right='#{hostname}'
  3. Reload the configuration

  4. From there, open a new pane and run

    $ tmux display -p '#{pane_pid} #{b:pane_tty} false false #h #D'
    25144 ttys007 false false MBP2018 %18
  5. Then from this pane, ssh into jumpbox

  6. Then open another pane and run

    $ cut -c3- ~/.tmux.conf | sh -sx _hostname 25144 ttys007 false false MBP2018 %18

    And paste the output in a comment.

Of course, you need to adapt 25144, ttys007, MBP2018, %18 with the values you got from step 4

TheZenTester commented 1 year ago

I appreciate the help!

  1. Linux localsystem 5.14.0-1056-oem #63-Ubuntu SMP Fri Dec 16 14:32:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Output from step 6:
❯ cut -c3- ~/.tmux.conf | sh -sx _hostname 2023933 11 false false localsystem %51                                                          ─╯
+ :
+ set -e
+ unset GREP_OPTIONS
+ export LC_NUMERIC=C
+ set +H
+ true
+ printf
+ sed -E s///
+ uname -s
+ _uname_s=Linux
+ tmux -V
+ awk {gsub(/[^0-9.]/, "", $2); print ($2+0) * 100}
+ _tmux_version=300
+ command -v pkill
+ _hostname 2023933 11 false false localsystem %51
+ pane_pid=2023933
+ pane_tty=11
+ ssh_only=false
+ full=false
+ h_or_H=localsystem
+ _pane_info 2023933 11
+ pane_pid=2023933
+ pane_tty=11
+ ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=
+ awk -v pane_pid=2023933
        ((/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]
        }

+ pane_info=2025223:zentester:ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=
+ command=zentester:ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=
+ command=ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=
+ _ssh_or_mosh_args ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=
+ printf %s
+ ssh_or_mosh_args=
+ [ -n  ]
+ _is_true false
+ [ xfalse = xtrue ]
+ [ xfalse = xyes ]
+ [ xfalse = x1 ]
+ hostname=localsystem
+ printf %s\n localsystem
localsystem
gpakosz commented 1 year ago

Something is making SSH introspection fail.

I find ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command= suspicious.

Are you sure the TTY is named "just" 11?

gpakosz commented 1 year ago

What if you run

$ tmux display -p '#{pane_tty}'
TheZenTester commented 1 year ago

Original command & output from step 4 in your original troubleshooting instructions

❯ tmux display -p '#{pane_pid} #{b:pane_tty} false false #h #D'                                                                          ─╯
2023933 11 false false localsystem %51

Output from tmux display:

❯ tmux display -p '#{pane_tty}'                                                                                                          ─╯
/dev/pts/11
gpakosz commented 1 year ago

Ok can you please paste the output of the following command then

$ ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=

Assuming the pane bound to /dev/pts/11 still contains an ssh process to jumpbox, otherwise update accordingly

gpakosz commented 1 year ago

There should be a pane that's running an ssh connection: let's assume it's /dev/pts/11 And another pane from which to launch the ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=

TheZenTester commented 1 year ago
❯ ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=                                            ─╯
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
zentester                      2023933    3738 -zsh
zentester                      2023995    3179 -zsh
zentester                      2024104    3179 -zsh
zentester                      2024106    3179 -zsh
zentester                      2024107 2023995 /home/zentester/.cache/gitstatus/gitstatusd-linux-x86_64 -G v1.5.4 -s -1 -u -1 -d -1 -c -1
zentester                      2066762 2023933 ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=

I have an ssh connection open within a different window of the same pane of which i'm running the ps command locally, and in a separate pane i have an ssh connection as well

gpakosz commented 1 year ago

As you can see, there's no ssh process associated with that TTY

gpakosz commented 1 year ago

Dammit I messed the instructions 😞 sorry

gpakosz commented 1 year ago

I edited the instructions above, so that people don't try to debug with them

TheZenTester commented 1 year ago

Attempted again with updated instructions, output below:

❯ cut -c3- ~/.tmux.conf | sh -sx _hostname 2068966 11 false false localsystem %56                                                          ─╯
+ :
+ set -e
+ unset GREP_OPTIONS
+ export LC_NUMERIC=C
+ set +H
+ true
+ printf
+ sed -E s///
+ uname -s
+ _uname_s=Linux
+ tmux -V
+ awk {gsub(/[^0-9.]/, "", $2); print ($2+0) * 100}
+ _tmux_version=300
+ command -v pkill
+ _hostname 2068966 11 false false localsystem %56
+ pane_pid=2068966
+ pane_tty=11
+ ssh_only=false
+ full=false
+ h_or_H=localsystem
+ _pane_info 2068966 11
+ pane_pid=2068966
+ pane_tty=11
+ ps -t 11 --sort=lstart -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=
+ awk -v pane_pid=2068966
        ((/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]
        }

+ pane_info=2069450:zentester:ssh -p 222 zentester@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ command=zentester:ssh -p 222 zentester@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ command=ssh -p 222 zentester@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ _ssh_or_mosh_args ssh -p 222 zentester@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ printf %s ssh -p 222 zentester@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ perl -n -e print if s/.*?\bssh[\w]*\s*((?:\s+-\w+)*)(\s+\w+)(\s\w+)?/\1\2/
+ args= -p 222@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ printf %s  -p 222@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ ssh_or_mosh_args= -p 222@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ [ -n  -p 222@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so ]
+ ssh -G -p 222@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ awk /^hostname / { print $2; exit }
+ hostname=
+ [ -z  ]
+ ssh -T -o ControlPath=none -o ProxyCommand=sh -c 'echo %%hostname%% %h >&2' -p 222@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so
+ awk /^%hostname% / { print $2; exit }
+ hostname=
+ _is_true false
+ [ xfalse = xtrue ]
+ [ xfalse = xyes ]
+ [ xfalse = x1 ]
+ printf %s\n
gpakosz commented 1 year ago

Alright, we can see that the ssh process was found:

pane_info=2069450:zentester:ssh -p 222 zentester@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so

But the regex that tries to isolate the args fails

$ printf '%s' 'ssh -p 222 zentester@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so' | perl -n -e 'print if s/.*?\bssh[\w]*\s*((?:\s+-\w+)*)(\s+\w+)(\s\w+)?/\1\2/'
 -p 222@jumpbox -i /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so

Let me debug that

gpakosz commented 1 year ago

Can you please try the gh-627 branch?

TheZenTester commented 1 year ago

Looks like it worked!!!! Thank you for the quick and expedient help @gpakosz

gpakosz commented 1 year ago

That being said, your ssh invocation should likely be -I /usr/lib/x86_64-linux-gnu/pkcs11/opensc-pkcs11.so with an upper case letter and not -i

ww7 commented 8 months ago

Hi, on macOS (latest 'Sonoma') resolving of SSH hostname not working, tried hostname_full_ssh, hostname_ssh and hostname. Printing empty value.

In same time this ssh -G $ssh_or_mosh_args 2>/dev/null | awk '/^hostname / { print $2; exit }'is printing remote hostname

ww7 commented 8 months ago

pane_info='25239:ww7:zsh (cwterm)', seems no SSH process associated with that TTY

based on tmux display -p '#{pane_pid} #{b:pane_tty} false false #h #D' before SSH

cut -c3- ~/.tmux.conf | sh -sx _hostname 25239 ttys005 false false mbp21 %0
+ :
+ set -e
+ unset GREP_OPTIONS
+ export LC_NUMERIC=C
+ LC_NUMERIC=C
+ set +H
+ set +H
+ printf ''
+ sed -E s///
++ uname -s
+ _uname_s=Darwin
+ '[' -z /private/tmp/tmux-502/default,24922,0 ']'
+ '[' -z /private/tmp/tmux-502/default ']'
+ '[' -z /opt/homebrew/Cellar/tmux/3.3a_3/bin/tmux ']'
+ '[' /opt/homebrew/Cellar/tmux/3.3a_3/bin/tmux = tmux ']'
++ tmux -V
++ /opt/homebrew/Cellar/tmux/3.3a_3/bin/tmux -S /private/tmp/tmux-502/default -V
++ awk '{gsub(/[^0-9.]/, "", $2); print ($2+0) * 100}'
+ _tmux_version=330
+ command -v pkill
+ _hostname 25239 ttys005 false false mbp21 %0
+ pane_pid=25239
+ pane_tty=ttys005
+ ssh_only=false
+ full=false
+ h_or_H=mbp21
++ _pane_info 25239 ttys005
++ pane_pid=25239
++ pane_tty=ttys005
++ case "$_uname_s" in
++ ps -t ttys005 -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=
++ awk -v pane_pid=25239 '
        ((/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]
        }
      '
+ pane_info='25239:ww7:zsh (cwterm)'
+ command='ww7:zsh (cwterm)'
+ command='zsh (cwterm)'
++ _ssh_or_mosh_args 'zsh (cwterm)'
++ case "$1" in
++ printf %s ''
+ ssh_or_mosh_args=
+ '[' -n '' ']'
+ _is_true false
+ '[' false = true ']'
+ '[' false = yes ']'
+ '[' false = 1 ']'
+ hostname=mbp21
+ printf '%s\n' mbp21
mbp21

and based on tmux display -p '#{pane_pid} #{b:pane_tty} false false #h #D' after SSH

cut -c3- ~/.tmux.conf | sh -sx _hostname 3657818 5 false false a %1
+ :
+ set -e
+ unset GREP_OPTIONS
+ export LC_NUMERIC=C
+ LC_NUMERIC=C
+ set +H
+ set +H
+ printf ''
+ sed -E s///
++ uname -s
+ _uname_s=Darwin
+ '[' -z /private/tmp/tmux-502/default,24922,0 ']'
+ '[' -z /private/tmp/tmux-502/default ']'
+ '[' -z /opt/homebrew/Cellar/tmux/3.3a_3/bin/tmux ']'
+ '[' /opt/homebrew/Cellar/tmux/3.3a_3/bin/tmux = tmux ']'
++ tmux -V
++ /opt/homebrew/Cellar/tmux/3.3a_3/bin/tmux -S /private/tmp/tmux-502/default -V
++ awk '{gsub(/[^0-9.]/, "", $2); print ($2+0) * 100}'
+ _tmux_version=330
+ command -v pkill
+ _hostname 3657818 5 false false a %1
+ pane_pid=3657818
+ pane_tty=5
+ ssh_only=false
+ full=false
+ h_or_H=a
++ _pane_info 3657818 5
++ pane_pid=3657818
++ pane_tty=5
++ case "$_uname_s" in
++ ps -t 5 -o user=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -o pid= -o ppid= -o command=
++ awk -v pane_pid=3657818 '
        ((/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: /dev/tty5 and /dev/5: No such file or directory
+ pane_info=3657818::
+ command=:
+ command=
++ _ssh_or_mosh_args ''
++ case "$1" in
++ printf %s ''
+ ssh_or_mosh_args=
+ '[' -n '' ']'
+ _is_true false
+ '[' false = true ']'
+ '[' false = yes ']'
+ '[' false = 1 ']'
+ hostname=a
+ printf '%s\n' a
a (full is a.ww7.work)
ww7 commented 7 months ago

@gpakosz I'm sorry, can you please take a look?

gpakosz commented 7 months ago

@ww7 👋

Please open a dedicated issue