Detect SSH sessions without affecting performance

Exclude local runs by KITTY_PID.
Check KITTY_WINDOW_ID to detect connections via ssh kitten.
Check SSH via who -m with the integration manually installed and sudo.
This commit is contained in:
pagedown 2022-03-07 12:00:55 +08:00
parent 43fd8cb13c
commit 817ac82968
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB
2 changed files with 21 additions and 3 deletions

View File

@ -155,8 +155,17 @@ _ksi_main() {
fi fi
if [[ "${_ksi_prompt[title]}" == "y" ]]; then if [[ "${_ksi_prompt[title]}" == "y" ]]; then
if [[ -n "$SSH_TTY$SSH2_TTY" ]]; then if [[ -n "$KITTY_PID" ]]; then
# kitty running locally
elif [[ -n "$SSH_TTY" || -n "$SSH2_TTY$KITTY_WINDOW_ID" ]]; then
# connected to most SSH servers
# or use ssh kitten to connected to some SSH servers that do not set SSH_TTY
_ksi_prompt[hostname_prefix]="\h: "; _ksi_prompt[hostname_prefix]="\h: ";
elif [[ -n "$(builtin command -v who)" ]]; then
# the shell integration script is installed manually on the remote system
# the environment variables are cleared after sudo
# OpenSSH's sshd creates entries in utmp for every login so use those
[[ "$(builtin command who -m 2> /dev/null)" =~ "\([a-fA-F.:0-9]+\)$" ]] && _ksi_prompt[hostname_prefix]="\h: ";
fi fi
# see https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html#Controlling-the-Prompt # see https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html#Controlling-the-Prompt
# we use suffix here because some distros add title setting to their bashrc files by default # we use suffix here because some distros add title setting to their bashrc files by default

View File

@ -233,8 +233,17 @@ _ksi_deferred_init() {
# (LF becomes \n, etc.). This isn't necessary in precmd because (%) does it # (LF becomes \n, etc.). This isn't necessary in precmd because (%) does it
# for us. # for us.
builtin local is_ssh_session="n" builtin local is_ssh_session="n"
if [[ -n "$SSH_TTY$SSH2_TTY" ]]; then if [[ -n "$KITTY_PID" ]]; then
is_ssh_session="y"; # kitty running locally
elif [[ -n "$SSH_TTY" || -n "$SSH2_TTY$KITTY_WINDOW_ID" ]]; then
# connected to most SSH servers
# or use ssh kitten to connected to some SSH servers that do not set SSH_TTY
is_ssh_session="y"
elif [[ -n "$(builtin command -v who)" ]]; then
# the shell integration script is installed manually on the remote system
# the environment variables are cleared after sudo
# OpenSSH's sshd creates entries in utmp for every login so use those
[[ "$(builtin command who -m 2> /dev/null)" =~ "\([a-fA-F.:0-9]+\)$" ]] && is_ssh_session="y"
fi fi
if [[ "$is_ssh_session" == "y" ]]; then if [[ "$is_ssh_session" == "y" ]]; then