Fix terminal title after SSH remote logging to another machine
Solution: add some functions ~/.bashrc to do something after ssh and su commands
function title()
{
# change the title of the current window or tab
echo -ne "\033]0;$*\007"
}
function ssh()
{
/usr/bin/ssh "$@"
# revert the window title after the ssh command
title $USER@$HOST
}
function su()
{
/bin/su "$@"
# revert the window title after the su command
title $USER@$HOST
}
Note: restart bash after edits to ~/.bashrc
Example:
# title is "user1@boxA"
ssh boxB # auto changes title while on boxB to "user1@boxB"
exit
# title returns back to "user1@boxA" because of our title call in ssh()
su - user2 # auto changes title while switched user to user2: "user2@boxA"
exit
# title returns back to "user1@boxA" because of our title call in su()
Hope that helps.
I don't know about window titles, but I have been trying to have my system do something on terminating a ssh session—actually, after terminating a ssh session. In short: it doesn't work like that. Basically you have three choices:
Write a wrapper around ssh, i.e., an executable shell script named
ssh
that takes precedence over/usr/bin/ssh
in your $PATH which contains the lineexec /usr/bin/ssh $@
somewhere in its middle. This enables you to have your shell do some stuff before and after the effective ssh binary is run, while keeping th eoverhead to a minimum.Write a patch against the SSH sources of your choice to provide you a cleanup hook that executes a shell command passed via commandline or some config-setting. That's what we want.
Have
PROMPT_COMMAND
evaluate the output ofhistory
. Basically a more generic and more ugly approach to 1.
Configure your local shell dotfile (e.g. $PROMPT_COMMAND
in ~/.bashrc
) to set the terminal title appropriately, using the same mechanism.
For example:
export PROMPT_COMMAND="printf '\e]0;bash\7\n'"